Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to display Cash Return in two decimal places. I tried using .toFixed however, it's not working. I am trying to display 1 million as 1.00 or 10 million as 10.00 and so forth but always in 2 decimal place.

What I have tried:

var Return = document.getElementById("Cash").value;
var CashRate = document.getElementById("CashRate").value;

aCashReturnCompensation = (Return/1000000);

CashRate =   Math.pow(1+CashRate/100,1/365)-1;
document.getElementById("CashReturn").value=Math.floor(Return/Math.pow(1+CashRate,periods);
Posted
Updated 17-May-18 7:53am
v5
Comments
RedDk 16-May-18 14:30pm    
Example:

var number = 55.4301;

to reduce the precision ... use:

number.toPrecision(2)

which will give you:

55.43
W Balboos, GHB 17-May-18 9:20am    
A bit of a warning for your future coding.

Although, in javaScript it doesn't matter, there is a difference as to how calculations will end up if you use 1000000 and not 1000000.0 - in how they are stored, which number is first in a calculation if you mix types, and any number of other rules for a language implementation.

Round off errors during calculations could give you unexpected results.
Member 13289364 17-May-18 10:53am    
Thank you. I will keep that in mind.
Richard Deeming 17-May-18 10:54am    
"It's not working" is not a helpful description of the problem.

Gazing into my crystal ball, I'm guess that CashReturn is an <input type="number" />.

Furthermore, I'm guessing that you're using Firefox, which unfortunately doesn't support fixed decimals in numeric inputs at the moment. There's a bug report[^] for this, but it's been open for 4 years with no sign of a resolution.

(If that's not the problem, then you need to update your question to provide a proper description of the problem.)

1 solution

This worked for me.
/1000000).toFixed(2);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900