-1

I'm trying to convert string into number but it is adding exponential symbol. I'm expecting the same output but in number not in string.

I have tried toFixed(), but it is returning string.

For example

var x = "0.00000001";
+x.toFixed(8);
console.log(x); // Actual Output: "0.00000001" | Expected output: 0.00000001
Sample code
var x = "0.00000001";
+x; // output: 1e-8

Actual Output: "0.00000001"

Expected output: 0.00000001

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 3
    that is just how JavaScript expresses large (as in lots of digits) numbers. it's still just a plain old number. you can do math with it or convert it back to a string to view it in long form. – I wrestled a bear once. Aug 26 '19 at 12:39
  • 5
    `0.00000001` *is* `1e-8`. It's literally the same value. If you need the value formatted for display, then use a string, if you want to use mathematical operations, then it doesn't matter how JS formats the numeric value. – VLAZ Aug 26 '19 at 12:40

2 Answers2

1

If you print a number without formatting, the javascript interpeter decides the format. It tries to select a short format with respect to the precision. So 0.00000001 is printed as 1e-8.

Code analysis:

var x = "0.00000001";

Here yo assign a string to x.

+x.toFixed(8);

This should throw an error for the part x.toFixed(8), because toFixed() is not defined for string. Maybe you wanted:

(+x).toFixed(8);

The plus sign converts the string into a number, and then toFixed(8) creates a formatted string for that number. The result is not used in any way (expect console output).

console.log(x);

In the line before, you didn't changed the value of x. So it's still the assigned string of the first line and you get the correct "0.00000001"

Wiimm
  • 2,971
  • 1
  • 15
  • 25
  • Your result is same as input, which is wrong because this will again returns the string not the number. – Harsh Gupta Aug 28 '19 at 07:31
  • Do you read my comment especially the last sentence? You assigned a string to x, don't modify x and print x. So the output of x is still a string. And the debugging (!) function `console.log()` reflects the string type by quotes. If you assign x to a input element like `input.value = x` the input box don't show the quotes. – Wiimm Aug 29 '19 at 13:19
-1

try this

  var oldString = '0.00000001';
  var oldNumber = Number('0.00000001');
   const str = oldNumber.toString();

    const exponent = parseInt(str.split('-')[1], 10);
    const result = oldNumber.toFixed(exponent);
    console.log(result);
ANIK ISLAM SHOJIB
  • 3,002
  • 1
  • 27
  • 36
  • How does this "fix" the problem? `oldNumber` is still represented as `1e-8`. The OP already knows how to use `toFixed` to get a string representation. – Heretic Monkey Aug 26 '19 at 12:47
  • Old Number doesn't but result does it is explained here follow them for better explanations https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript – ANIK ISLAM SHOJIB Aug 26 '19 at 12:51
  • 2
    Answers should be self-contained; I should be able to understand your answer and how it relates to the question without visiting another question. Also, that question has nothing to do with this one; it's about floating point accuracy when doing mathematical operations. This one demonstrates a lack of understanding of how numbers are stored in JavaScript. – Heretic Monkey Aug 26 '19 at 12:55
  • sorry for the inconvenience I will get back to you soon – ANIK ISLAM SHOJIB Aug 26 '19 at 13:12
  • I have already tried `.toFixed()` and the problem is it will return output string datatype but I need number datatype. – Harsh Gupta Aug 28 '19 at 11:54