printf("%'d", 1234567890);
def ts x; x.to_s =~ /^([^.]*\d)(\d{3}(\.|$))/ ? x = ts($1)+","+$2+$' : x; end
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 が上書きされるので、計算順序に注意しながら若干工夫する必要があるみたい。
#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(); }
a = 123456789 format(a, ",") # or format(a, "_")Python 3.6 で導入された「フォーマット済み文字列リテラル」を用いると、以下のように書くことも出来る。
a = 123456789 f'{a:,}' # or f'{a:_}'また、「数値リテラル」では整数、浮動小数点数共に "_" が桁区切り文字として無視されるので、以下のように数字の間の任意の位置に書くことができる。
a = 1_23_456_7890