零后舍入值

编程入门 行业动态 更新时间:2024-10-25 06:33:47
本文介绍了零后舍入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嘿伙计们, 我有一个长度转换器,转换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>在消息中写道 news:11 ********************** @ g43g2000cwa.googlegr oups ... "joshb" <jb****@gmail> wrote in message news:11**********************@g43g2000cwa.googlegr oups... 嘿伙计们, 我有一个长度转换器可以转换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.

更多推荐

零后舍入值

本文发布于:2023-11-02 08:07:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后舍

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!