天文学的に大きな数値を C/C++ で人間が読める形式に変換する

StackOverflow https://stackoverflow.com/questions/1499156

  •  19-09-2019
  •  | 
  •  

質問

私のプログラムは、100363443 など、最大 1 兆までの巨大な数値を出力しますが、それを読むのは難しいため、任意の数値を読みやすい形式で出力したいと考えています。

今私が使っているのは

printf ("%10ld", number);

フォーマット

printfを使用して結果の数値をいただければ幸いです。私のコードのほとんどは C++ ですが、既に printf を持っているので std::cout を導入したくありません。

ありがとう

役に立ちましたか?

解決

規格外のものを使用する apostrophe そのオプションが利用可能で、移植性が多少失われることを気にしない場合は、printf 形式文字列にフラグを追加します。

私のドキュメントによると、 ' フラグは以下で利用可能です POSIX 1997 年以来のシステム。

Unix、Linux、Mac などを使用している場合...問題ないはずです
Windows、DOS、iSeries、Android などを使用している場合...すべての賭けは外れます (ただし、システムに POSIX レイヤーをインストールできるかもしれません)。

#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;
}

私のシステムでは、このプログラムは以下を生成します。

# %'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

他のヒント

k、m などの接尾辞を使用して下位の桁を除外する humanize_number() を使用できます。これは標準的なルーチンではないため、リンクしたソースを d/l する必要があります。(2 条項 BSD ライセンス、あらゆる種類の使用を許可します。)

Humanize_number のマニュアルページ.

Humanize_number ソース コード から NetBSD.

HUMANIZE_NUMBER(3)      NetBSD Library Functions Manual     HUMANIZE_NUMBER(3)

NAME
     dehumanize_number, humanize_number -- format a number into a human read-
     able form and viceversa

SYNOPSIS
     #include <stdlib.h>

     int
     dehumanize_number(const char *str, int64_t *result);

     int
     humanize_number(char *buf, size_t len, int64_t number,
         const char *suffix, int scale, int flags);

これは、次のようにサフィックスを追加することで機能します。

       Suffix    Description    Multiplier
       k         kilo           1024
       M         mega           1048576
       G         giga           1073741824
       T         tera           1099511627776
       P         peta           1125899906842624
       E         exa            1152921504606846976

簡単な方法は、出力の直前に double に変換し、指数科学表記法で出力する %e を使用することです。これを試して:

double n = (double)number;
printf("%10.0e", n);
std::cout << std::setprecision(5) << std::scientific << 100363443.0;

数値は浮動小数点であることに注意してください

編集:または、科学的なものが好きではない場合は、ネットで次のようなものを見つけました。

struct comma : public std::numpunct<char>
{ 
    protected: std::string do_grouping() const { return "\003" ; } 
};

std::cout.imbue( std::locale( std::cout.getloc(), new comma ) );
std::cout << 100363443 << std::endl;

編集2:Jerry が指摘したように、上記のようなカンマ クラスは必要ありません。これだけで十分だと思われます (ただし、大きな数値をまったくフォーマットしないロケールもあると思われますが?):

std::cout.imbue( std::locale( "" ) );
std::cout << 100363443 << std::endl;

ローカリゼーションを忘れないでください (特にライブラリを作成している場合)。
ヨーロッパ (英国を除く) では、1,000,000 ではなく 1,000,000 になります。

これは、ロケールを使用せずに直接 C で書いた例です。ポジティブな場合にのみ機能します。(「DiscoVlad」さんに大変お世話になりました)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <strings.h>


void my_reverse ( char* s ) {
    int c, i, j;
    for (i=0, j= strlen(s)-1;i<j;i++,j--) {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}


char* insert_commas(unsigned long long input ) {
    int i, intlen;
    char* buffer;
    char* formatted;

    intlen = (int) ceil(log10(input * 1.0));
    buffer = (char *) malloc((intlen + 1) * sizeof(char));

    sprintf(buffer, "%llu", input);  // build buffer
    formatted = (char *) malloc((intlen + (int) ceil(intlen/3.0)) * sizeof(char));  // malloc output buffer
    my_reverse(buffer);

    for(i=intlen; i>=0; i--) {
        formatted[strlen(formatted)] = buffer[i];
        if (i%3 == 0 && i<intlen && i > 0) {
            formatted[strlen(formatted)] = ',';
        }
    }
    free(buffer);

    return formatted;
}


int main() {
    char* formatted;

    // don't forget to free(formatted) after each call.
    formatted = insert_commas(123);
    printf("output %s\n", formatted);
    // output 123

    formatted = insert_commas(1234);
    printf("output %s\n", formatted);
    // output 1,234

    formatted = insert_commas(123456);
    printf("output %s\n", formatted);
    // output 123,456

    formatted = insert_commas(1234567);
    printf("output %s\n", formatted);
    // output 1,234,567

    formatted = insert_commas(123456789);
    printf("output %s\n", formatted);
    // output 123,456,789

    formatted = insert_commas(12345678901234567890ull);
    printf("output %s\n", formatted);
    // output 12,345,678,901,234,567,890

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top