将数字格式化为字符串时删除整数

发布于 2024-12-18 01:54:05 字数 149 浏览 2 评论 0原文

将数字格式化为字符串时,我可以使用 %.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遮了一弯 2024-12-25 01:54:05

我认为唯一的方法是使用数学删除小数点之前的数字,或者事后进行字符串操作,例如 componentsSeparatedByString:@"."

删除小数点之前的数字的简单数学方法这将是:

float initialValue = 100.943f;
int beforeDecimal = (int)initialValue;
float afterDecimal = initialValue - beforeDecimal;

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:

float initialValue = 100.943f;
int beforeDecimal = (int)initialValue;
float afterDecimal = initialValue - beforeDecimal;
身边 2024-12-25 01:54:05

使用 NSNumberFormatter,并设置其maximumIntegerDigits 为 0:

float f = 123.456;
NSNumber * nF = [NSNumber numberWithFloat:f];
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumIntegerDigits:0];
NSLog(@"%@", [formatter stringFromNumber:nF]);

2011-11-23 18:36:59.138 NoIntegerDigits[21834:903] .456

Use an NSNumberFormatter, and set its maximumIntegerDigits to 0:

float f = 123.456;
NSNumber * nF = [NSNumber numberWithFloat:f];
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumIntegerDigits:0];
NSLog(@"%@", [formatter stringFromNumber:nF]);

2011-11-23 18:36:59.138 NoIntegerDigits[21834:903] .456

凉薄对峙 2024-12-25 01:54:05

好吧,我想我现在得到了你的问题。

这是另一个解决方案,你只能从字符串中获取字符串。

示例:

NSString *floatAsString = @"123.456";
NSArray stringDivisions = [floatAsString componentsSeparatedByString:@"."];

这里您将在数组中得到的是:123 和 456 来分隔字符串对象。
一旦你得到了两个独立的弦,你就可以用它们来演奏了。

这就是你所需要的?

Ok I guess i got your question now..

Here is another solution where you will get strings out of string only.

example:

NSString *floatAsString = @"123.456";
NSArray stringDivisions = [floatAsString componentsSeparatedByString:@"."];

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 ?

悟红尘 2024-12-25 01:54:05

简单的方法来做到这一点,
这是一个例子,

首先将字符串转换为浮点值。

浮点x = 102.943;

int y = x;

浮动 ans = x % y;

NSLog(@"\nx : %f \ny : %d \nans : %f", x, y, ans);

所以输出将是:

x:102.943000

y:102

答:0.943000

希望这是您所需要的。

Simple way to do that,
Here is an example,

Firstly convert your string to float value.

float x = 102.943;

int y = x;

float ans = x % y;

NSLog(@"\nx : %f \ny : %d \nans : %f", x, y, ans);

So the output will be:

x : 102.943000

y : 102

ans : 0.943000

Hope this is what you needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文