0

I have large decimal numbers which I am getting from a request & I want to convert them to string. So for EG:

I tried all methods converting to string

var r=12311241412412.1241523523523235
        r.toString(); 
        r+'';
        ''+r;
        String(r);
//output
'12311241412412.1241'
//what i want
'12311241412412.1241523523523235'

All methods return the decimal numbers upto 4 digits (12311241412412.1241) but i want all the number till end. I also tried r.toFixed().toString() but each time the length of decimal numbers change.

What would be easy way to do this?

Ritwikga
  • 33
  • 1
  • 5

1 Answers1

2

the problem is that 12311241412412.1241523523523235 in javascript means 12311241412412.125. whatever you do is not gonna work unless you put the whole thing in a string at the first place.

use this instead:

var r = "12311241412412.1241523523523235";
The Moisrex
  • 1,857
  • 1
  • 14
  • 16
  • OP said that he is getting it from the request – Sagar V Mar 26 '17 at 13:13
  • @SagarV : Right..!! var r = request.id /// response – Ritwikga Mar 26 '17 at 13:26
  • first log on console the value and make sure you got it properly – Sagar V Mar 26 '17 at 13:27
  • @Ritwikga What is `request`, where does it come from? Who generated that number value? JS *cannot represent* such precise numbers. – Bergi Mar 26 '17 at 13:41
  • it does not matter where it gets it from. as long as it's a Number javascript will change it to what you see once it parsed. So only way to make it the original value is to just not parse it at all (keeping it as a string). if you want mathematical operations, then you can use libraries that are out there just for this purpose. – The Moisrex Mar 26 '17 at 16:37