alexfusion
Programmer
Hello everyone,
Simple problem:
I have a variable that is adding charges,but the result has to contain always 2 decimals.
Let'say:
charges+=0.254444444;
I use the following to get 2 decimals:
charges=Math.round(charges*100)/100;
The problem is when the sum gives a result such as 1 or 1.5.
"zeros" to the right are truncated.
I need always 2 decimals,I mean: 1.00 or 1.50 intead of 1 or 1.5.
Is there a "fast way" of doing this that I don't know? or do I get the lenght of the string after the period and add the "zeros" acording to the result?I think this solution is not very elegant.I mean if the result is 1.5 then I add a "0" to the result,displaying 1.50.
If anyone has some advice,very much appreciated.
Thanks in advance.
alexfusion
Simple problem:
I have a variable that is adding charges,but the result has to contain always 2 decimals.
Let'say:
charges+=0.254444444;
I use the following to get 2 decimals:
charges=Math.round(charges*100)/100;
The problem is when the sum gives a result such as 1 or 1.5.
"zeros" to the right are truncated.
I need always 2 decimals,I mean: 1.00 or 1.50 intead of 1 or 1.5.
Is there a "fast way" of doing this that I don't know? or do I get the lenght of the string after the period and add the "zeros" acording to the result?I think this solution is not very elegant.I mean if the result is 1.5 then I add a "0" to the result,displaying 1.50.
If anyone has some advice,very much appreciated.
Thanks in advance.
alexfusion