PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

[FIXED] How to convert a very large decimal number to a string?

 August 11, 2022     decimal, javascript, numbers, string     No comments   

Issue

I want to convert the decimal number 123.456e+304 to the string "123.456e+304" (the position of the decimal point . should not be changed).

Here is what I've tried, but all of them return the same result: 1.23456e+306 (the position of the decimal point has been changed, and it has also replaced 304 with 306).

var e = 123.456e+304;

console.log(e.toString());
console.log((e).toString());
console.log(e .toString());
console.log(e.toFixed());
console.log(String(e));
console.log((new String(e)).toString());
console.log(`${e}`);
console.log('' + e);
console.log(e + '');
console.log(''.split.call(e, '').join(''));

Is there any way to convert it to the expect string "123.456e+304"?


Solution

It is not possible, because once you assigned:

var e = 123.456e+304;

and is processed by the JavaScript interpreter, e is just a number internally represented by IEEE 754 a number equivalent to 1.23456e+306 and it has "no memory" where your decimal point was. So no matter what you do, you can't know where the decimal point was and let you move it to where you want it to be in a string.

In order "to have the knowledge" of where the decimal point was, you need to have a string to begin with, but that's also the result that you want.



Answered By - nonopolarity
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,206,464

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing