如何修剪小数点后的零

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

trig = [currentVal]

我尝试在小数点后修剪零,如下所示。 doubleValue]; trig = trig / 100; NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init]; [formatter setMaximumFractionDigits:0]; display.text = [formatter stringFromNumber:[NSNumber numberWithDouble:trig]];

小数点后仍未显示数字。

这里 currentVal 是我进入的号码。

例如,如果我通过trig= 123(在执行trig / 100之后,最初的trig= 123我想显示1.23,但显示为1.23000000)。

解决方案

从文档,它看起来像 setFractionDigits: / code>只是用于转换的方式。

最好的办法是在格式化之前将数字转换为整数,例如

double converted = round(trig); // man round for docs

您也可以使用 stringWithFormat : of NSString ,但是你将失去所有的本地化优势 NSNumberFormatter 。

I am trying to trim zeros after a decimal point as below but it's not giving desired result.

trig = [currentVal doubleValue]; trig = trig/100; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setMaximumFractionDigits:0]; display.text = [formatter stringFromNumber:[NSNumber numberWithDouble:trig]];

The number is still being displayed without trimming zeros after the decimal point.

Here currentVal is the number I am entering.

For example if i pass "trig" = 123 (Initially "trig" = 123 after doing trig/100 i want to display 1.23 but it is displaying as 1.23000000).

解决方案

From the documentation, it looks like setFractionDigits: is only for converting the other way.

The best thing to do is probably to convert your number to an integer before formatting it e.g.

double converted = round(trig); // man round for docs

You can use also the formatting functions of stringWithFormat: of NSString, but then you will lose all the localisation advantages you get with NSNumberFormatter.

更多推荐

如何修剪小数点后的零

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

发布评论

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

>www.elefans.com

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