Number to string problem

nativescriptrocks
nativescriptcore
plugins

#1

i have found that when we want to store textfield number input to database. then we must parse it to number like

var num = Integer.ParseInt(textfieldvalue);

but there are no way to convert pure number into string.

i want to store a id in database like
EST_000001

var num = 0000001;

i can hardcode it like

var x = “EST_”+num;

but in db it will store like `

EST_1

` instead of

EST_000001

can anyone guide me on this.


#2

No matter how many zeros (0) you add before a number, it will be ignored - that doesn’t add any value to a number mathematically as simple as that.

It’s not NativeScript, simply thats how JavaScript / Java or I believe any other language works.


#3

is there any way to achieve this???


#4

If you want to achieve this, just works with padded number.

import { padStart } from 'lodash';

const targetLengthID = 7;
const num = 1;
const x = ['EST', padStart(num, targetLengthID, '0')].join('_');
// x = "EST_0000001"

#5

nope its not working …!

var observableModule = require("data/observable");
var padStart  = require(".././lodash");

function HomeViewModel() {
  var viewModel = observableModule.fromObject({

    onButtonTap: function () {
      console.log("Button was pressed");
      const targetLengthID = 7;
      const num = 1;
      const x = ['EST', padStart(num, targetLengthID, '0')].join('_');
      console.log(x);
    },
      
  });

  return viewModel;
}

module.exports = HomeViewModel;

output:

[Redmi]: Button was pressed
[Redmi]: EST_1