对大整数进行舍入 - Objective-C

发布于 2024-10-12 11:03:29 字数 196 浏览 3 评论 0原文

我如何在 Objective-C 中实现以下目标?

从 0 到 999,999,999 之间的整数 x 开始。 最终得到一个整数 y。

如果 x 介于 0 到 9999 之间,则 y = x 否则,x 会变成 45k(代表 45,000)或 998m(代表 9.98 亿)。换句话说,使用字符“k”和“m”以使 y 的长度小于或等于 4 个字符。

How might I achieve the following in objective-c?

Start with an integer x between 0 and 999,999,999.
And end up with an integer y.

If x is between 0 and 9999, then y = x
Otherwise, x becomes something like 45k (representing 45,000) or 998m (representing 998 million). In other words, use the characters "k" and "m" in order to keep y under or equal to 4 characters long.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

私藏温柔 2024-10-19 11:03:29

不舍入,只是截断:

NSString *text;
if (x >= 1000000) text = [NSString stringWithFormat:@"%dm",x/1000000];
else if (x >= 1000) text = [NSString stringWithFormat:@"%dk",x/1000];
else text = [NSString stringWithFormat:@"%d",x];

舍入解决方案:

NSString *text;
if (x >= 1000000) text = [NSString stringWithFormat:@"%.0fm",x/1000000.0f];
else if (x >= 1000) text = [NSString stringWithFormat:@"%.0fk",x/1000.0f];
else text = [NSString stringWithFormat:@"%d",x];

如果想要更高的精度,请使用 float%.1f 等。

Doesn't round but simply cuts off:

NSString *text;
if (x >= 1000000) text = [NSString stringWithFormat:@"%dm",x/1000000];
else if (x >= 1000) text = [NSString stringWithFormat:@"%dk",x/1000];
else text = [NSString stringWithFormat:@"%d",x];

Rounding solution:

NSString *text;
if (x >= 1000000) text = [NSString stringWithFormat:@"%.0fm",x/1000000.0f];
else if (x >= 1000) text = [NSString stringWithFormat:@"%.0fk",x/1000.0f];
else text = [NSString stringWithFormat:@"%d",x];

If you want greater precision, use float and %.1f etc.

星星的軌跡 2024-10-19 11:03:29
thousands = x / 1000;
millions = thousands / 1000;
billions = millions / 1000;

if( billions )
  sprintf(y, "%dB", billions);
else if( millions)
  sprintf(y, "%dM", millions);
else if( thousands )
  sprintf(y, "%dK", thousands);
else 
  sprntf(y, "%d", x);
thousands = x / 1000;
millions = thousands / 1000;
billions = millions / 1000;

if( billions )
  sprintf(y, "%dB", billions);
else if( millions)
  sprintf(y, "%dM", millions);
else if( thousands )
  sprintf(y, "%dK", thousands);
else 
  sprntf(y, "%d", x);
亽野灬性zι浪 2024-10-19 11:03:29
NSString *y = nil;

// If Billion
if (x >= 1000000000) y = [NSString stringWithFormat:@"%.0fB", x/1000000000];
// If Million
else if (x >= 1000000) y = [NSString stringWithFormat:@"%.0fM", x/1000000];
// If it has more than 4 digits
else if (x >= 10000) y = [NSString stringWithFormat:@"%.0fK", x/1000];
// If 4 digits or less
else y = [NSString stringWithFormat:@"%d", x];

更新:添加了缺少的“else”,否则无法获取小于 4 位的数字。

NSString *y = nil;

// If Billion
if (x >= 1000000000) y = [NSString stringWithFormat:@"%.0fB", x/1000000000];
// If Million
else if (x >= 1000000) y = [NSString stringWithFormat:@"%.0fM", x/1000000];
// If it has more than 4 digits
else if (x >= 10000) y = [NSString stringWithFormat:@"%.0fK", x/1000];
// If 4 digits or less
else y = [NSString stringWithFormat:@"%d", x];

Update: Added the missing 'else', otherwise it wouldn't get numbers with less than 4 digits.

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