Thousands Separator と言うか、いわゆる 3 桁区切りとか 1,000 区切りとか千の位のコンマと言うか、位取りと言うか
2010-12-07 現在 Ubuntu 10.04 で確認する限りにおいて Ruby 1.8.7 の format では SUSv2 の flag には対応出来てない模様
関数でやるとこんな感じか?
関数でやるとこんな感じか?
def ts x; x.to_s =~ /^([^.]*\d)(\d{3}(\.|$))/ ? x = ts($1)+","+$2+$' : x; end
Ruby 版のデッドコピーだけどこんな感じか?
function ts(x){ var y,z; return x.toString().match(/^([^.]*\d)(\d{3}(\.|$))/) ? (y = RegExp.$2, z = RegExp.rightContext, x = ts(RegExp.$1) + "," + y + z) : x; }再帰すると RegExp が上書きされるので、計算順序に注意しながら若干工夫する必要があるみたい。
std::ostringstream に std::locale 食わせると良いらしい。
多分こんな感じ。
参考:
追記: 2018-10-15
Cygwin だと libstdc++ のコンパイルオプションが腐ってるらしく std::locale が "C" と "POSIX" 以外食わせると runtime_exception 吐くので、現時点でこの方法は使えない模様。
関連:
多分こんな感じ。
#include <string> #include <sstream> #include <locale> template<class T> inline const std::string ts(T v) { std::ostringstream oss; oss.imbue(std::locale("")); oss << std::fixed << v; return oss.str(); }
参考:
- stackoverflow / 2016-11-16: Format thousands separator
- Yahoo!知恵袋 / 2016-12-14: Microsoft Visual C++ 2010 の MFC を使っています。…
追記: 2018-10-15
Cygwin だと libstdc++ のコンパイルオプションが腐ってるらしく std::locale が "C" と "POSIX" 以外食わせると runtime_exception 吐くので、現時点でこの方法は使えない模様。
関連:
String::Format Method を使うと Excel の書式指定と同じ方式で出来るようだ。
C++ でも CLR でコンパイルすると使えるっぽいけど。
- MSDN Library / NET Development / .NET Framework 4.6 and 4.5 / Development Guide / Application Essentials / Base Types / Formatting Types / Custom Numeric Format Strings # The "," Custom Specifier
C++ でも CLR でコンパイルすると使えるっぽいけど。
コメントをかく