naichi's lab

3日後の自分(他人)への書き置き

Unity、インフレクリッカー系ゲームによくある200kとか4.21mみたいな数値表示をしてみた

概要

f:id:naichilab:20180729024425p:plain

  • クリッカー系ゲームによくある、 20k 3.56m 521b みたいな数値表示をできるようにしてみた。

こういう表記法?ってなんていうんですかね? ググれば出て来そうだけどググり方分からなくて自分で作ってしまった。
=> ざっくりと human readable string で良いみたいです。@shienaさんありがとうございました。

仕様

  • なるべく多くの桁数を扱いたかったので、ベースは double です。
    • 扱える範囲は 1.7 × 10 ^ 308 までだそうです。ざっくり308桁。(ってことで合ってる?よく分からん)
  • 専用の数値クラスを作るの面倒だったので、double型を文字列化するだけにとどめました。
  • ゲーム内のコイン等で使う予定なので、マイナスは扱いません。0として文字列化されます。
  • 付与するサフィックスは以下の通りです。

    数値 サフィックス
    0〜 なし
    1,000 10^3〜 k - killo
    1,000,000 10^6〜 m - million
    1,000,000,000 10^9〜 b - billion
    1,000,000,000,000 10^12〜 t - trillion
    (省略) 10^15〜 A
    (省略) 10^18〜 B
    (省略) 10^21〜 C
    ・・・ ・・・
    ・・・ Z
    ・・・ AA
    ・・・ AB
    ・・・ ・・・

ソースコード

GitHubで公開してます。

naichilab/unity-human-readable-number - GitHub

導入方法

こちらから unitypackage をダウンロードしてUnityプロジェクトに放り込めば動くと思います。

Releases · naichilab/unity-human-readable-number · GitHub

もしくは DoubleExtensions.cs だけコピペして作ってもいいです。

使い方

//using naichilab.Scripts.Extensions;

double coin;
coin = 12345;
Debug.Log(coin.ToReadableString()); // 12.3k

coin = 8454645;
Debug.Log(coin.ToReadableString()); // 8.45m

coin = 4.25e10; //42,500,000,000
Debug.Log(coin.ToReadableString()); // 42.5b

coin = 7.342e60; //7,342,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
Debug.Log(coin.ToReadableString()); // 7.34P

その他

  • 動けばいいやで書いたのでパフォーマンスとか分かりません。
  • もっといい方法あるよって方はおしえてください〜。
  • 不具合と改善点あれば連絡いただくか直接GitHubにIssue・PRください〜

おまけ

double.MaxValue まで出力できることを確認しました。

0 => 0.00E0 => 0
1 => 1.00E0 => 1
2 => 2.00E0 => 2
1.23456789 => 1.23E0 => 1
12.3456789 => 1.23E1 => 12
123.456789 => 1.23E2 => 123
1234.56789 => 1.23E3 => 1.23k
12345.6789 => 1.23E4 => 12.3k
123456.789 => 1.23E5 => 123k
1234567.89 => 1.23E6 => 1.23m
12345678.9 => 1.23E7 => 12.3m
123456789 => 1.23E8 => 123m
1234567890 => 1.23E9 => 1.23b
12345678900 => 1.23E10 => 12.3b
123456789000 => 1.23E11 => 123b
1234567890000 => 1.23E12 => 1.23t
12345678900000 => 1.23E13 => 12.3t
123456789000000 => 1.23E14 => 123t
1.23456789E+15 => 1.23E15 => 1.23A
1.23456789E+16 => 1.23E16 => 12.3A
1.23456789E+17 => 1.23E17 => 123A
1.23456789E+18 => 1.23E18 => 1.23B
1.23456789E+19 => 1.23E19 => 12.3B
1.23456789E+20 => 1.23E20 => 123B
1.23456789E+21 => 1.23E21 => 1.23C
1.23456789E+22 => 1.23E22 => 12.3C
1.23456789E+23 => 1.23E23 => 123C
1.23456789E+24 => 1.23E24 => 1.23D
1.23456789E+25 => 1.23E25 => 12.3D
1.23456789E+26 => 1.23E26 => 123D
1.23456789E+27 => 1.23E27 => 1.23E
1.23456789E+28 => 1.23E28 => 12.3E
1.23456789E+29 => 1.23E29 => 123E
1.23456789E+30 => 1.23E30 => 1.23F
1.23456789E+31 => 1.23E31 => 12.3F
1.23456789E+32 => 1.23E32 => 123F
1.23456789E+33 => 1.23E33 => 1.23G
1.23456789E+34 => 1.23E34 => 12.3G
1.23456789E+35 => 1.23E35 => 123G
1.23456789E+36 => 1.23E36 => 1.23H
1.23456789E+37 => 1.23E37 => 12.3H
1.23456789E+38 => 1.23E38 => 123H
1.23456789E+39 => 1.23E39 => 1.23I
1.23456789E+40 => 1.23E40 => 12.3I
1.23456789E+41 => 1.23E41 => 123I
1.23456789E+42 => 1.23E42 => 1.23J
1.23456789E+43 => 1.23E43 => 12.3J
1.23456789E+44 => 1.23E44 => 123J
1.23456789E+45 => 1.23E45 => 1.23K
1.23456789E+46 => 1.23E46 => 12.3K
1.23456789E+47 => 1.23E47 => 123K
1.23456789E+48 => 1.23E48 => 1.23L
1.23456789E+49 => 1.23E49 => 12.3L
1.23456789E+50 => 1.23E50 => 123L
1.23456789E+51 => 1.23E51 => 1.23M
1.23456789E+52 => 1.23E52 => 12.3M
1.23456789E+53 => 1.23E53 => 123M
1.23456789E+54 => 1.23E54 => 1.23N
1.23456789E+55 => 1.23E55 => 12.3N
1.23456789E+56 => 1.23E56 => 123N
1.23456789E+57 => 1.23E57 => 1.23O
1.23456789E+58 => 1.23E58 => 12.3O
1.23456789E+59 => 1.23E59 => 123O
1.23456789E+60 => 1.23E60 => 1.23P
1.23456789E+61 => 1.23E61 => 12.3P
1.23456789E+62 => 1.23E62 => 123P
1.23456789E+63 => 1.23E63 => 1.23Q
1.23456789E+64 => 1.23E64 => 12.3Q
1.23456789E+65 => 1.23E65 => 123Q
1.23456789E+66 => 1.23E66 => 1.23R
1.23456789E+67 => 1.23E67 => 12.3R
1.23456789E+68 => 1.23E68 => 123R
1.23456789E+69 => 1.23E69 => 1.23S
1.23456789E+70 => 1.23E70 => 12.3S
1.23456789E+71 => 1.23E71 => 123S
1.23456789E+72 => 1.23E72 => 1.23T
1.23456789E+73 => 1.23E73 => 12.3T
1.23456789E+74 => 1.23E74 => 123T
1.23456789E+75 => 1.23E75 => 1.23U
1.23456789E+76 => 1.23E76 => 12.3U
1.23456789E+77 => 1.23E77 => 123U
1.23456789E+78 => 1.23E78 => 1.23V
1.23456789E+79 => 1.23E79 => 12.3V
1.23456789E+80 => 1.23E80 => 123V
1.23456789E+81 => 1.23E81 => 1.23W
1.23456789E+82 => 1.23E82 => 12.3W
1.23456789E+83 => 1.23E83 => 123W
1.23456789E+84 => 1.23E84 => 1.23X
1.23456789E+85 => 1.23E85 => 12.3X
1.23456789E+86 => 1.23E86 => 123X
1.23456789E+87 => 1.23E87 => 1.23Y
1.23456789E+88 => 1.23E88 => 12.3Y
1.23456789E+89 => 1.23E89 => 123Y
1.23456789E+90 => 1.23E90 => 1.23Z
1.23456789E+91 => 1.23E91 => 12.3Z
1.23456789E+92 => 1.23E92 => 123Z
1.23456789E+93 => 1.23E93 => 1.23AA
1.23456789E+94 => 1.23E94 => 12.3AA
1.23456789E+95 => 1.23E95 => 123AA
1.23456789E+96 => 1.23E96 => 1.23AB
1.23456789E+97 => 1.23E97 => 12.3AB
1.23456789E+98 => 1.23E98 => 123AB
1.23456789E+99 => 1.23E99 => 1.23AC
1.23456789E+100 => 1.23E100 => 12.3AC
1.23456789E+101 => 1.23E101 => 123AC
1.23456789E+102 => 1.23E102 => 1.23AD
1.23456789E+103 => 1.23E103 => 12.3AD
1.23456789E+104 => 1.23E104 => 123AD
1.23456789E+105 => 1.23E105 => 1.23AE
1.23456789E+106 => 1.23E106 => 12.3AE
1.23456789E+107 => 1.23E107 => 123AE
1.23456789E+108 => 1.23E108 => 1.23AF
1.23456789E+109 => 1.23E109 => 12.3AF
1.23456789E+110 => 1.23E110 => 123AF
1.23456789E+111 => 1.23E111 => 1.23AG
1.23456789E+112 => 1.23E112 => 12.3AG
1.23456789E+113 => 1.23E113 => 123AG
1.23456789E+114 => 1.23E114 => 1.23AH
1.23456789E+115 => 1.23E115 => 12.3AH
1.23456789E+116 => 1.23E116 => 123AH
1.23456789E+117 => 1.23E117 => 1.23AI
1.23456789E+118 => 1.23E118 => 12.3AI
1.23456789E+119 => 1.23E119 => 123AI
1.23456789E+120 => 1.23E120 => 1.23AJ
1.23456789E+121 => 1.23E121 => 12.3AJ
1.23456789E+122 => 1.23E122 => 123AJ
1.23456789E+123 => 1.23E123 => 1.23AK
1.23456789E+124 => 1.23E124 => 12.3AK
1.23456789E+125 => 1.23E125 => 123AK
1.23456789E+126 => 1.23E126 => 1.23AL
1.23456789E+127 => 1.23E127 => 12.3AL
1.23456789E+128 => 1.23E128 => 123AL
1.23456789E+129 => 1.23E129 => 1.23AM
1.23456789E+130 => 1.23E130 => 12.3AM
1.23456789E+131 => 1.23E131 => 123AM
1.23456789E+132 => 1.23E132 => 1.23AN
1.23456789E+133 => 1.23E133 => 12.3AN
1.23456789E+134 => 1.23E134 => 123AN
1.23456789E+135 => 1.23E135 => 1.23AO
1.23456789E+136 => 1.23E136 => 12.3AO
1.23456789E+137 => 1.23E137 => 123AO
1.23456789E+138 => 1.23E138 => 1.23AP
1.23456789E+139 => 1.23E139 => 12.3AP
1.23456789E+140 => 1.23E140 => 123AP
1.23456789E+141 => 1.23E141 => 1.23AQ
1.23456789E+142 => 1.23E142 => 12.3AQ
1.23456789E+143 => 1.23E143 => 123AQ
1.23456789E+144 => 1.23E144 => 1.23AR
1.23456789E+145 => 1.23E145 => 12.3AR
1.23456789E+146 => 1.23E146 => 123AR
1.23456789E+147 => 1.23E147 => 1.23AS
1.23456789E+148 => 1.23E148 => 12.3AS
1.23456789E+149 => 1.23E149 => 123AS
1.23456789E+150 => 1.23E150 => 1.23AT
1.23456789E+151 => 1.23E151 => 12.3AT
1.23456789E+152 => 1.23E152 => 123AT
1.23456789E+153 => 1.23E153 => 1.23AU
1.23456789E+154 => 1.23E154 => 12.3AU
1.23456789E+155 => 1.23E155 => 123AU
1.23456789E+156 => 1.23E156 => 1.23AV
1.23456789E+157 => 1.23E157 => 12.3AV
1.23456789E+158 => 1.23E158 => 123AV
1.23456789E+159 => 1.23E159 => 1.23AW
1.23456789E+160 => 1.23E160 => 12.3AW
1.23456789E+161 => 1.23E161 => 123AW
1.23456789E+162 => 1.23E162 => 1.23AX
1.23456789E+163 => 1.23E163 => 12.3AX
1.23456789E+164 => 1.23E164 => 123AX
1.23456789E+165 => 1.23E165 => 1.23AY
1.23456789E+166 => 1.23E166 => 12.3AY
1.23456789E+167 => 1.23E167 => 123AY
1.23456789E+168 => 1.23E168 => 1.23AZ
1.23456789E+169 => 1.23E169 => 12.3AZ
1.23456789E+170 => 1.23E170 => 123AZ
1.23456789E+171 => 1.23E171 => 1.23BA
1.23456789E+172 => 1.23E172 => 12.3BA
1.23456789E+173 => 1.23E173 => 123BA
1.23456789E+174 => 1.23E174 => 1.23BB
1.23456789E+175 => 1.23E175 => 12.3BB
1.23456789E+176 => 1.23E176 => 123BB
1.23456789E+177 => 1.23E177 => 1.23BC
1.23456789E+178 => 1.23E178 => 12.3BC
1.23456789E+179 => 1.23E179 => 123BC
1.23456789E+180 => 1.23E180 => 1.23BD
1.23456789E+181 => 1.23E181 => 12.3BD
1.23456789E+182 => 1.23E182 => 123BD
1.23456789E+183 => 1.23E183 => 1.23BE
1.23456789E+184 => 1.23E184 => 12.3BE
1.23456789E+185 => 1.23E185 => 123BE
1.23456789E+186 => 1.23E186 => 1.23BF
1.23456789E+187 => 1.23E187 => 12.3BF
1.23456789E+188 => 1.23E188 => 123BF
1.23456789E+189 => 1.23E189 => 1.23BG
1.23456789E+190 => 1.23E190 => 12.3BG
1.23456789E+191 => 1.23E191 => 123BG
1.23456789E+192 => 1.23E192 => 1.23BH
1.23456789E+193 => 1.23E193 => 12.3BH
1.23456789E+194 => 1.23E194 => 123BH
1.23456789E+195 => 1.23E195 => 1.23BI
1.23456789E+196 => 1.23E196 => 12.3BI
1.23456789E+197 => 1.23E197 => 123BI
1.23456789E+198 => 1.23E198 => 1.23BJ
1.23456789E+199 => 1.23E199 => 12.3BJ
1.23456789E+200 => 1.23E200 => 123BJ
1.23456789E+201 => 1.23E201 => 1.23BK
1.23456789E+202 => 1.23E202 => 12.3BK
1.23456789E+203 => 1.23E203 => 123BK
1.23456789E+204 => 1.23E204 => 1.23BL
1.23456789E+205 => 1.23E205 => 12.3BL
1.23456789E+206 => 1.23E206 => 123BL
1.23456789E+207 => 1.23E207 => 1.23BM
1.23456789E+208 => 1.23E208 => 12.3BM
1.23456789E+209 => 1.23E209 => 123BM
1.23456789E+210 => 1.23E210 => 1.23BN
1.23456789E+211 => 1.23E211 => 12.3BN
1.23456789E+212 => 1.23E212 => 123BN
1.23456789E+213 => 1.23E213 => 1.23BO
1.23456789E+214 => 1.23E214 => 12.3BO
1.23456789E+215 => 1.23E215 => 123BO
1.23456789E+216 => 1.23E216 => 1.23BP
1.23456789E+217 => 1.23E217 => 12.3BP
1.23456789E+218 => 1.23E218 => 123BP
1.23456789E+219 => 1.23E219 => 1.23BQ
1.23456789E+220 => 1.23E220 => 12.3BQ
1.23456789E+221 => 1.23E221 => 123BQ
1.23456789E+222 => 1.23E222 => 1.23BR
1.23456789E+223 => 1.23E223 => 12.3BR
1.23456789E+224 => 1.23E224 => 123BR
1.23456789E+225 => 1.23E225 => 1.23BS
1.23456789E+226 => 1.23E226 => 12.3BS
1.23456789E+227 => 1.23E227 => 123BS
1.23456789E+228 => 1.23E228 => 1.23BT
1.23456789E+229 => 1.23E229 => 12.3BT
1.23456789E+230 => 1.23E230 => 123BT
1.23456789E+231 => 1.23E231 => 1.23BU
1.23456789E+232 => 1.23E232 => 12.3BU
1.23456789E+233 => 1.23E233 => 123BU
1.23456789E+234 => 1.23E234 => 1.23BV
1.23456789E+235 => 1.23E235 => 12.3BV
1.23456789E+236 => 1.23E236 => 123BV
1.23456789E+237 => 1.23E237 => 1.23BW
1.23456789E+238 => 1.23E238 => 12.3BW
1.23456789E+239 => 1.23E239 => 123BW
1.23456789E+240 => 1.23E240 => 1.23BX
1.23456789E+241 => 1.23E241 => 12.3BX
1.23456789E+242 => 1.23E242 => 123BX
1.23456789E+243 => 1.23E243 => 1.23BY
1.23456789E+244 => 1.23E244 => 12.3BY
1.23456789E+245 => 1.23E245 => 123BY
1.23456789E+246 => 1.23E246 => 1.23BZ
1.23456789E+247 => 1.23E247 => 12.3BZ
1.23456789E+248 => 1.23E248 => 123BZ
1.23456789E+249 => 1.23E249 => 1.23CA
1.23456789E+250 => 1.23E250 => 12.3CA
1.23456789E+251 => 1.23E251 => 123CA
1.23456789E+252 => 1.23E252 => 1.23CB
1.23456789E+253 => 1.23E253 => 12.3CB
1.23456789E+254 => 1.23E254 => 123CB
1.23456789E+255 => 1.23E255 => 1.23CC
1.23456789E+256 => 1.23E256 => 12.3CC
1.23456789E+257 => 1.23E257 => 123CC
1.23456789E+258 => 1.23E258 => 1.23CD
1.23456789E+259 => 1.23E259 => 12.3CD
1.23456789E+260 => 1.23E260 => 123CD
1.23456789E+261 => 1.23E261 => 1.23CE
1.23456789E+262 => 1.23E262 => 12.3CE
1.23456789E+263 => 1.23E263 => 123CE
1.23456789E+264 => 1.23E264 => 1.23CF
1.23456789E+265 => 1.23E265 => 12.3CF
1.23456789E+266 => 1.23E266 => 123CF
1.23456789E+267 => 1.23E267 => 1.23CG
1.23456789E+268 => 1.23E268 => 12.3CG
1.23456789E+269 => 1.23E269 => 123CG
1.23456789E+270 => 1.23E270 => 1.23CH
1.23456789E+271 => 1.23E271 => 12.3CH
1.23456789E+272 => 1.23E272 => 123CH
1.23456789E+273 => 1.23E273 => 1.23CI
1.23456789E+274 => 1.23E274 => 12.3CI
1.23456789E+275 => 1.23E275 => 123CI
1.23456789E+276 => 1.23E276 => 1.23CJ
1.23456789E+277 => 1.23E277 => 12.3CJ
1.23456789E+278 => 1.23E278 => 123CJ
1.23456789E+279 => 1.23E279 => 1.23CK
1.23456789E+280 => 1.23E280 => 12.3CK
1.23456789E+281 => 1.23E281 => 123CK
1.23456789E+282 => 1.23E282 => 1.23CL
1.23456789E+283 => 1.23E283 => 12.3CL
1.23456789E+284 => 1.23E284 => 123CL
1.23456789E+285 => 1.23E285 => 1.23CM
1.23456789E+286 => 1.23E286 => 12.3CM
1.23456789E+287 => 1.23E287 => 123CM
1.23456789E+288 => 1.23E288 => 1.23CN
1.23456789E+289 => 1.23E289 => 12.3CN
1.23456789E+290 => 1.23E290 => 123CN
1.23456789E+291 => 1.23E291 => 1.23CO
1.23456789E+292 => 1.23E292 => 12.3CO
1.23456789E+293 => 1.23E293 => 123CO
1.23456789E+294 => 1.23E294 => 1.23CP
1.23456789E+295 => 1.23E295 => 12.3CP
1.23456789E+296 => 1.23E296 => 123CP
1.23456789E+297 => 1.23E297 => 1.23CQ
1.23456789E+298 => 1.23E298 => 12.3CQ
1.23456789E+299 => 1.23E299 => 123CQ
1.23456789E+300 => 1.23E300 => 1.23CR
1.23456789E+301 => 1.23E301 => 12.3CR
1.23456789E+302 => 1.23E302 => 123CR
1.23456789E+303 => 1.23E303 => 1.23CS
1.23456789E+304 => 1.23E304 => 12.3CS
1.23456789E+305 => 1.23E305 => 123CS
1.23456789E+306 => 1.23E306 => 1.23CT
1.23456789E+307 => 1.23E307 => 12.3CT
1.23456789E+308 => 1.23E308 => 123CT
1.79769313486232E+308 => 1.80E308 => 180CT