零后舍入值 [英] Rounding values after zeros

查看:85
本文介绍了零后舍入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,


我有一个长度转换器,转换mm,cm,m,km,ft,yd,in和

mi。如果我将5毫米转换为英里,我得到0.00000031068559649 ...里程。我是

希望将我的所有值转换为两个小数点,但如果它来了
up零,我想在最后一个零之后舍入到两个点。我已经通过谷歌查看了
还没找到任何我可以使用的东西,所以我想b / b
想知道你们是否有任何脚本或任何线索我可以使用

制作它。


提前谢谢,

Josh。

Hey guys,

I have a length converter which converts mm, cm, m, km, ft, yd, in and
mi. If I convert 5mm to miles, I get 0.00000031068559649... miles. I''m
looking to convert all my values to two decimal spots, but if it comes
up zeros, I would like to round to two spots after the last zero. I''ve
looked through google and haven''t found anything I could use yet, so I
was wondering if you guys had any scripts or any leads I could use to
make it.

Thanks in advance,
Josh.

推荐答案

" joshb" < JB **** @ gmail.com>在消息中写道

news:11 ********************** @ g43g2000cwa.googlegr oups.com ...
"joshb" <jb****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
嘿伙计们,

我有一个长度转换器可以转换mm,cm,m,km,ft,yd,in和
mi。如果我将5毫米转换为英里,我得到0.00000031068559649 ...里程。我想要将我的所有值转换为两个小数点,但是如果它来了零,我想在最后一个零之后舍入到两个点。我已经浏览了谷歌并且还没找到任何我可以使用的东西,所以我想知道你们是否有任何脚本或任何我可以使用的线索来制作它。
Hey guys,

I have a length converter which converts mm, cm, m, km, ft, yd, in and
mi. If I convert 5mm to miles, I get 0.00000031068559649... miles. I''m
looking to convert all my values to two decimal spots, but if it comes
up zeros, I would like to round to two spots after the last zero. I''ve
looked through google and haven''t found anything I could use yet, so I
was wondering if you guys had any scripts or any leads I could use to
make it.




尝试添加有效数字或精确或你的搜索结果。


我快速浏览了一下,我认为

yournumber.toPrecision(2)

可能会这样做。


例如:0.00000031068559649

应该变成3.1e-7



Try adding "significant digits" or "precision" to your searches.

I had a quick look around and I think
yournumber.toPrecision(2)
might do it.

eg: 0.00000031068559649
should become 3.1e-7


其实我觉得josh想要精度要灵活取决于

数字输出多远。我没有看到任何方法来实现这个

而不使用RegEx。


mi =" 0.00000031068559649" ;;

mi_formatted = mi.match(/.+?\.0 +。{2} /); //返回0.00000031


我不知道这个RegEx对你的项目有多可靠,但它应该是一个好的开始。

是一个好的开始。

Actually I think josh wants the precision to be flexiable depending on
how far the digits go out. I don''t see any way to accomplish this
without the use of a RegEx.

mi = "0.00000031068559649";
mi_formatted = mi.match(/.+?\.0+.{2}/); // returns 0.00000031

I don''t know how reliable this RegEx for your project, but its should
be a good start.


为了记录,是的,我想将0.000043874

转换成0.000044.


感谢Nathan的提示,我现在有一些工作要做。

For the record, yeah, I''m looking to convert something like 0.000043874
into 0.000044.

Thanks for the tip Nathan, I have something to work with now.


这篇关于零后舍入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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