Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

javascript - 如何在javascript中格式化浮点数?(How to format a float in javascript?)

In JavaScript, when converting from a float to a string, how can I get just 2 digits after the decimal point?(在JavaScript中,当从float转换为字符串时,如何在小数点后只获得2位数?)

For example, 0.34 instead of 0.3445434.(例如,0.34而不是0.3445434。)   ask by translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are functions to round numbers.(有圆形数字的功能。)

For example:(例如:) var x = 5.0364342423; print(x.toFixed(2)); will print 5.04.(将打印5.04。) EDIT: Fiddle(编辑: 小提琴)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...