2
var y = 123e5;
console.log(y); // output: 12300000

Above code has a number variable y and the output I get is 12300000. I would like to show it same but as a string. Output should be '123e5' and the type of value also be string. The requirement for such question is that I do not want JS to manipulate my data until I require And If it does than there might also be a way to get the exact value I stored/wrote.

Test values for y can also be taken as 1023e5, 1023e+5, 1023e+5, 10230e+5.

<!DOCTYPE html>
<html>
<body>

<pre>
<script>
var y = 123e5;
document.writeln(y);
document.writeln(123e5);
document.writeln(123e+5);
document.writeln(123e-5);
document.writeln(1230e-5);
</script>
</pre>

</body>
</html>

My exact objective was to know that if there is any way I can get the same value stored/wrote without change.So what is the exact way to get my original value.

Thanks!

Raj
  • 570
  • 5
  • 15

1 Answers1

3

My exact objective was to know that if there is any way I can get the same value stored/wrote without change.So what is the exact way to get my original value.

No, that information is lost. The scientific notation in JavaScript is to make it clearer to express some difficult numbers (very big/small) in source code. If you intend to always know how the value was defined in its specific notation, you will need to use strings or attach some extra information to store it.

However, here is a function that converts trailing 0 of a number into the eN equivalent, how the notation works in JavaScript with numbers.

var convertNumToExpStr = function(number) {
  return String(number).replace(/0+$/, function(all) {
    return 'e' + all.length;
  });
};

document.body.textContent = convertNumToExpStr(123e5);
alex
  • 479,566
  • 201
  • 878
  • 984
  • @MatíasFidemraizer Sorry I don't post here often and haven't learned how that works yet. – alex Mar 07 '16 at 08:33
  • What if `convertNumToExpStr(123e5+1);` ? – hallucinations Mar 07 '16 at 08:35
  • @alex Thanks! you're almost there but if i change the value than it doesn't output as expected... Can you please check you code for the value `1023e5` – Raj Mar 07 '16 at 08:36
  • @hallucinations it wouldn't change the represented number as there isn't any trailing `0`. – alex Mar 07 '16 at 08:36
  • @MatíasFidemraizer I guess its better to give Fiddle link and add important code that adding entire code snippet in answer. But yes, this is for answers where code snippet is too long. – Rajesh Mar 07 '16 at 08:37
  • @RJ Works for me, gives `'1023e5'`. – alex Mar 07 '16 at 08:37
  • @alex does not work for me it gives `12300001`, i use fiddle haha https://jsfiddle.net/szdcgs54/ – Anonymous Duck Mar 07 '16 at 08:38
  • 1
    @Rajesh Nah, don't be lazy. It's **absolutely better to have the entire resource in SO in order to avoid that a downtime of external resources would be a problem for future readers when looking for a solution**. With a code snippet here you can even run it in place... – Matías Fidemraizer Mar 07 '16 at 08:38
  • @alex I've edited your answer to show you how it works. Feel free to check it and learn how you would do it yourself :) – Matías Fidemraizer Mar 07 '16 at 08:39
  • @MatíasFidemraizer twist you edited it wrong and break the answer. joke – Anonymous Duck Mar 07 '16 at 08:40
  • @EuphoriaGrogi That's the expected output because there are no trailing zeroes. – alex Mar 07 '16 at 08:41
  • we should shut up, this guy has 243K repu haha – Anonymous Duck Mar 07 '16 at 08:43
  • This works fine. My exact objective was to know that if there is any way I can get the same value stored/wrote without change. So the above code is good for a particular pattern but its not perfect to get the value of all other pattern like `1023e5`, `1023e+5`, `1023e+5`, `10230e+5` and so on... So what is the exact way to get my original value. I've also updated my Question – Raj Mar 07 '16 at 09:38
  • @RJ What are you doing exactly? The scientific notation in JavaScript is to make it clearer to express some numbers in source code. – alex Mar 07 '16 at 09:42