1
nStr    =   12.00;
alert(typeof(nStr));// getting number
x = nStr.split('.');
// Other than string Split function Through an error,
nStr    =   12.00;
nStr    +=  '';
alert(typeof(nStr));// getting string
x       =   nStr.split('.');
x1      =   x[0];
x2      =   x.length > 1 ? '.' + x[1] : '';
alert(x1+x2);

//Expected Output is 12.00

I got the temp output coding ( its not optimized coding)

nStr += '';
x = nStr.split('.');
x1  = x[0];
x[1]= (x[1].length == 1) ? '.' + x[1] + '0' : '.' + x[1];
x2  = x.length > 1 ? x[1] : '.00';
alert(x1+x2);               //  12.00
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
k6t back
  • 87
  • 1
  • 8
  • You're looking for a equilivant of printf(). This is a duplicate of http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format – g19fanatic Nov 16 '11 at 12:17

2 Answers2

5

12.00 is 12 you can't change that fact.

Looks like what you are after is the toFixed() method of JavaScript:

nStr    =   12;
alert(nStr.toFixed(2));

This will alert "12.00" as you want. The number passed to the method determines how many digits after the decimal point will be displayed.

Worth mentioning is that the toFixed method will also round numbers, for example if in the above example nStr will be 12.5283 it will show 12.53 in the alert.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0

Instead of writing

nStr    =   12.00;

u can write something like this

nStr    =   "12.00";