限制双到小数点后两位

编程入门 行业动态 更新时间:2024-10-23 02:06:40
本文介绍了限制双到小数点后两位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我如何实现从双下面的转换为字符串:

1.4324 => 1.439.4000 => 9.443.000 => 43

即。我要四舍五入到的小数位数,但不希望任何尾随零,也就是我不想

9.4 => 9.40(错)43.000 => 43.00(错)

所以这code,我现在不工作,因为它显示多余的零:

[的NSString stringWithFormat:@%2F,总]

解决方案

使用NSNumberFormatter.见的格式化编程指南上Number格式化的。

How do I achieve the following conversion from double to a string:

1.4324 => "1.43" 9.4000 => "9.4" 43.000 => "43"

i.e. I want to round to to decimal places but dont want any trailing zeros, ie i dont want

9.4 => "9.40" (wrong) 43.000 => "43.00" (wrong)

So this code which I have now doesn't work as it displays excess zeros:

[NSString stringWithFormat: @"%.2f", total]

解决方案

Use NSNumberFormatter. See the Data Formatting Programming Guide's chapter on Number Formatters.

更多推荐

限制双到小数点后两位

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

发布评论

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

>www.elefans.com

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