如何在javascript中格式化浮点数? [英] How to format a float in javascript?

查看:574
本文介绍了如何在javascript中格式化浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,当从float转换为字符串时,如何在小数点后只获得2位数?例如,0.34而不是0.3445434。

In JavaScript, when converting from a float to a string, how can I get just 2 digits after the decimal point? For example, 0.34 instead of 0.3445434.

推荐答案

var result = Math.round(original*100)/100;

详细信息,以防代码不能自我解释。

The specifics, in case the code isn't self-explanatory.

编辑:...或只是使用 toFixed ,由TimBüthe提出。忘了那个,谢谢(和upvote)提醒:)

edit: ...or just use toFixed, as proposed by Tim Büthe. Forgot that one, thanks (and an upvote) for reminder :)

这篇关于如何在javascript中格式化浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆