1

I am trying to get an 8 decimal output from the following function.

The following function multiplies an input by 2 and then updates this input with the wagerUpdate variable. I would like this outputted number to have 8 decimal places.

For example: if input number is 0.00000001 (this code is for a bitcoin website), then I would like output number to be 0.00000002. For some reason the code below is not working properly as the output number is in the format of 2e-8 without the .toFixed(8) code. Please help if you are able to. Thank you so much.

<script>
function MultiplyWagerFunction() {
var wager = document.getElementById("wagerInputBox").value;
var wagerUpdate = wager*2;
document.getElementById("wagerInputBox").value = +wagerUpdate.toFixed(8);  
}
</script>
Hyperius
  • 3
  • 2
  • Why are you doing `+wagerUpdate`? It's already a number by virtue of the fact you multiplied it by a number. Remove that and your `toFixed` should work. – Matt Burland May 03 '17 at 20:14
  • @MattBurland Post that as an answer. – Barmar May 03 '17 at 20:16
  • I have updated earlier question you had asked. Removed +. – Pankaj Shukla May 03 '17 at 20:22
  • 1
    Also, using javascript floating point arithmetic for financial calculations is [never a good idea](http://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript) (as you mentioned bitcoin). – sabithpocker May 03 '17 at 20:24

3 Answers3

1

If you remove the + before wagerUpdate.toFixed(8) it should work fine. wagerUpdate has already be converted to a number when you multiplied it by 2 so there should be no need for the unary +

var a = "0.00000001";
var b = a*2;
console.log(b.toFixed(8));
console.log(+b.toFixed(8));

^ see the difference.

The reason it doesn't work is because what you are doing is equivalent to:

+(b.toFixed(8))

because of the precedence of the operators (member access . is higher than unary +). You are converting b to a string with .toFixed and then converting it back into a number with + and then converting it back into a string again! (this time with the default toString behavior for numbers giving you exponential notation)

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • `toFixed(8)` converts it from a number to a string. The problem is that he really wants the string, he doesn't want to convert it back to a number, because when it gets converted back to a string to assign to `.value` it will use exponential notation instead of fixed notation. – Barmar May 03 '17 at 20:20
0

Just remove + from +wagerUpdate.toFixed(8); and you would be good.

Pankaj Shukla
  • 2,657
  • 2
  • 11
  • 18
  • Awesome. I changed it and everything is working well now. I am new to coding so this has really helped me. Thank you! – Hyperius May 03 '17 at 20:51
-3

Instead of:

document.getElementById("wagerInputBox").value = +wagerUpdate.toFixed(8);

try:

document.getElementById("wagerInputBox").innerHTML = +wagerUpdate.toFixed(8);

Why I say so is may be when you set value, browser tries to convert to best possible outcome. But, inner HTML should take the string equivalent!

praty
  • 535
  • 2
  • 9