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(); }