问题
I am trying to write a custom calculator but I am having trouble trying to work out a figure, I want to be able to add decimal points before the number which has been in puted.
For example if the user puts in 4 I want the value in the string to look like this 0.04 and so on 14 = 0.14, 100 = 1.00.
I tried using the inbuilt function
var num = 4;
fig = num.toFixed(2);
But that doesn't work, the only way I can think to do it is with if(val.length >2){ do something; } which would be a long way to do this. Has any body got any ideas for this?
Thanks a lot for your help
回答1:
>>> [4, 14, 100].map(function(n) { return (n / 100).toFixed(2) })
["0.04", "0.14", "1.00"]
回答2:
How about this?
var num = 4; var fig = num * 0.01;
来源:https://stackoverflow.com/questions/11377918/javascript-number-placing-and-how-to-4-0-04-14-0-14-100-1-00