Javascript number placing and how to , 4 = 0.04, 14 = 0.14, 100 = 1.00

空扰寡人 提交于 2019-12-03 01:04:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!