0

In Java script, I am using the below method to convert from number to string. In my case, number will be as float number or whole number (123.00 or 123.3456 or 567).

toString() -> it converts into whole number then converts into string. For Example:

 var totalAmount = 100.000;
   var str = totalAmount.toString();

     

output is 100

expected output is 100000

  var totalAmount1 = 123.456;
          var str1 = totalAmount.toString();
    
 

output is 123456 expected output is 123456 // Works as expected

I tried String() method also.

Any one help me how to get the expected result of 1st example such as "10000".

Muthukumar Marichamy
  • 1,175
  • 1
  • 17
  • 39
  • 2
    100.000000000 is still 100 in JS - as soon as you cast to number all trailing 0s are lost – mplungjan Jul 06 '21 at 17:37
  • (123.456).toString() is not 123456 – mplungjan Jul 06 '21 at 17:39
  • `(123.456).toString()` would be `"123.456"` not `"123456"`. If you want to convert to *thousands*, then multiply by `1000`. If you want to convert to a string and keep 3 decimal places, use `.toFixed(3)` – VLAZ Jul 06 '21 at 17:40
  • I want to as 123.000, out put should be as "123.000", then using regex I can remove the dot then again make it as number. Any idea ? – Muthukumar Marichamy Jul 06 '21 at 17:49
  • Depending on your culture/locale, you may find text representations of numbers using a decimal comma `,` instead of a decimal point `.` and a thousands separator of `.` instead of a comma or space. Within JavaScript itself, the numeric literal format is specified according to [the grammar](https://tc39.es/ecma262/#sec-literals-numeric-literals) with a decimal point `.` and optional `_` separators. e.g.: `Intl.NumberFormat('it').format(123_456.78) === '123.456,78'`. Parsing numbers by locale is [possible but more difficult](https://stackoverflow.com/a/45309230/1563833). – Wyck Jul 06 '21 at 19:09

0 Answers0