天文数字转换大量成C / C人类可读的形式++ [英] convert astronomically large numbers into human readable form in C/C++

查看:119
本文介绍了天文数字转换大量成C / C人类可读的形式++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序打印出巨大的数字 - 像100363443,高达一万亿 - 它那种难以阅读他们,所以我想打印易于阅读形式的任何数字。

My program prints out HUGE numbers - like 100363443, up to a trillion -- and it sort of hard to read them, so I would like to print any number in easy to read form.

现在我用

printf ("%10ld", number);

格式

我AP preciate一个结果数字用printf。我的大多数code是C ++但我并不想介绍的std ::法院,因为我已经有了的printf

感谢

推荐答案

使用非标准撇号标记中的格式化字符串,如果您有可用的选项,不介意丢失便携的一点点。

Use the non-standard apostrophe flag in the printf format string, if you have that option available and don't mind losing a little bit of portability.

据我的文档中, 标记可用于 POSIX 自1997年以来的系统。

According to my documentation, the ' flag is available for POSIX systems since 1997.

如果你是在Unix,Linux和Mac上,......你应该有
没有问题
如果您使用的是Windows,DOS,i系列,机器人,... ...所有的赌注都关闭(但也许你可以安装一个POSIX层到您的系统)。

If you are on Unix, Linux, Mac, ... you should have no problem
If you are on Windows, DOS, iSeries, Android, ... all bets are off (but maybe you can install a POSIX layer to your system).

#include <locale.h>
#include <stdio.h>

int main(void) {
  long int x = 130006714000000;

  setlocale(LC_NUMERIC, "en_US.utf-8"); /* important */
  while (x > 0) {
    printf("# %%'22ld: %'22ld\n", x); /* apostrophe flag */
    x *= 2; /* on my machine, the Undefined Behaviour for overflow
            // makes the number become negative with no ill effects */
  }
  return 0;
}

在我的系统这个程序生成:

On my system this program produces:

# %'22ld:    130,006,714,000,000
# %'22ld:    260,013,428,000,000
# %'22ld:    520,026,856,000,000
# %'22ld:  1,040,053,712,000,000
# %'22ld:  2,080,107,424,000,000
# %'22ld:  4,160,214,848,000,000
# %'22ld:  8,320,429,696,000,000
# %'22ld: 16,640,859,392,000,000
# %'22ld: 33,281,718,784,000,000
# %'22ld: 66,563,437,568,000,000
# %'22ld: 133,126,875,136,000,000
# %'22ld: 266,253,750,272,000,000
# %'22ld: 532,507,500,544,000,000
# %'22ld: 1,065,015,001,088,000,000
# %'22ld: 2,130,030,002,176,000,000
# %'22ld: 4,260,060,004,352,000,000
# %'22ld: 8,520,120,008,704,000,000

这篇关于天文数字转换大量成C / C人类可读的形式++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆