将数字格式化为字符串时删除整数
将数字格式化为字符串时,我可以使用 %.03f
限制小数点后的字符(在小数点后显示三位数字),但是如何删除小数点之前的所有数字观点?
例如,我希望将 100.943
格式化为 ".943"
。
When formatting a number into a string, I can limit the characters after a decimal point with, e.g., %.03f
(display three digits after the point), but how do I also remove all digits BEFORE the point?
For example, I'd like 100.943
to format to ".943"
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为唯一的方法是使用数学删除小数点之前的数字,或者事后进行字符串操作,例如
componentsSeparatedByString:@"."
删除小数点之前的数字的简单数学方法这将是:
I think the only way for you to do that would be to remove the number before the decimal point using math, or string manipulation after the fact, like
componentsSeparatedByString:@"."
A simple math way of removing it would be:
使用
NSNumberFormatter
,并设置其maximumIntegerDigits
为 0:Use an
NSNumberFormatter
, and set itsmaximumIntegerDigits
to 0:好吧,我想我现在得到了你的问题。
这是另一个解决方案,你只能从字符串中获取字符串。
示例:
这里您将在数组中得到的是:123 和 456 来分隔字符串对象。
一旦你得到了两个独立的弦,你就可以用它们来演奏了。
这就是你所需要的?
Ok I guess i got your question now..
Here is another solution where you will get strings out of string only.
example:
Here what you will get in array is: 123 and 456 to separate string objects.
Once you get two separate strings, you can play with them.
This is what you needed ?
简单的方法来做到这一点,
这是一个例子,
所以输出将是:
希望这是您所需要的。
Simple way to do that,
Here is an example,
So the output will be:
Hope this is what you needed.