是否有一个内置函数可以用逗号分隔C、C和x2B中的数字+;,还是JavaScript?
给定一个数字是否有一个内置函数可以用逗号分隔C、C和x2B中的数字+;,还是JavaScript?,javascript,c++,c,Javascript,C++,C,给定一个数字12456789,我需要在没有太多编码的情况下输出12456789。在C、C++、JavaScript中有没有内置函数可以用来做? 是的,这可以通过在区域设置正确的刻面来自动地在C++中完成。 #include <iostream> #include <locale> #include <string> template<typename CharT> struct Sep : public std::numpunct<Cha
12456789
,我需要在没有太多编码的情况下输出12456789
。在C、C++、JavaScript中有没有内置函数可以用来做? 是的,这可以通过在区域设置正确的刻面来自动地在C++中完成。
#include <iostream>
#include <locale>
#include <string>
template<typename CharT>
struct Sep : public std::numpunct<CharT>
{
virtual std::string do_grouping() const {return "\003";}
};
int main()
{
std::cout.imbue(std::locale(std::cout.getloc(), new Sep <char>()));
std::cout << 123456789 << "\n";
}
#包括
#包括
#包括
模板
结构Sep:public std::numpunct
{
虚拟std::string do_grouping()常量{return“\003”;}
};
int main()
{
std::cout.imbue(std::locale(std::cout.getloc(),new Sep());
std::cout我发现这个小javascript函数可以工作():
在某些C编译器实现中,为printf
family函数提供了扩展,以便在数字格式说明符中用作修饰符的单引号/撇号字符将执行“千分分组”:
#include <stdio.h>
#include <locale.h>
int main(void)
{
printf( "%'d\n", 1234567);
setlocale(LC_NUMERIC, "en_US");
printf( "%'d\n", 1234567);
return 0;
}
遗憾的是,这个扩展并没有得到特别广泛的支持。C++中的你通常会使用这样的东西:
std::locale loc("");
std::cout.imbue(loc);
std::cout << 1234567;
std::locale loc(“”);
标准:电流注入(loc);
std::cout//另一个javascript方法,用于数字
Number.prototype.withCommas= function(){
return String(this).replace(/\B(?=(?:\d{3})+(?!\d))/g,',')
}
var n=12456789.25;
alert(n.withCommas());
/*返回值:(字符串)
12,456,789.25
*/< P> C++,可以尝试:
std::locale get_numpunct_locale(std::locale locale = std::locale(""))
{
#if defined(_MSC_VER) && defined(_ADDFAC) && (!defined(_CPPLIB_VER) || _CPPLIB_VER < 403)
// Workaround for older implementations
std::_ADDFAC(locale, new std::numpunct<char>());
return locale;
#else
return std::locale(locale, new std::numpunct<TCHAR>());
#endif
}
template<class T>
std::string nformat(T value)
{
std::ostringstream ss;
ss.imbue(get_numpunct_locale());
ss << value;
return ss.str();
}
std::locale get\u numpunct\u locale(std::locale=std::locale(“”)
{
#如果已定义(_MSC_VER)和已定义(_ADDFAC)和(_CPPLIB_VER)| | CPPLIB_VER<403)
//旧版本实现的变通方法
std::_ADDFAC(locale,new std::numpunct());
返回区域设置;
#否则
返回std::locale(locale,newstd::numpunct());
#恩迪夫
}
模板
标准::字符串格式(T值)
{
std::ostringstream ss;
ss.imbue(get_numpunt_locale());
SS,谢谢,我发现了同样的事情从不死鱼中学习。+ 1不能比这更容易:)我喜欢C++ ^ ^这不是一个精确的答案,它取决于系统上的默认区域。<代码> STD::LOCAL LOC(“EnUn.UTF-8”);可能更健壮。@ PrimeReigPyuuin:我想这取决于你如何定义“鲁棒”。“有时可能会做一些令人满意的事情,但有时会完全失败”,那么是的,这是准确的(例如,minGW失败)。但答案的要点是,大多数时候,您应该尝试根据用户的需求定制输出,这是无名语言环境试图提供的。这比这里接受的答案要好,因为没有广泛的研究和调整,答案是不起作用的。我在VS19上,我有一个错误,说“错误C2039:\u ADDFAC
:不是std
的成员。你知道为什么吗?@SandraK:你的\u CPPLIB版本是什么?不确定,我怎么检查?@SandraK:\35; include
int main(){std::cout Ah…650:D。。
std::locale loc("");
std::cout.imbue(loc);
std::cout << 1234567;
Number.prototype.withCommas= function(){
return String(this).replace(/\B(?=(?:\d{3})+(?!\d))/g,',')
}
var n=12456789.25;
alert(n.withCommas());
std::locale get_numpunct_locale(std::locale locale = std::locale(""))
{
#if defined(_MSC_VER) && defined(_ADDFAC) && (!defined(_CPPLIB_VER) || _CPPLIB_VER < 403)
// Workaround for older implementations
std::_ADDFAC(locale, new std::numpunct<char>());
return locale;
#else
return std::locale(locale, new std::numpunct<TCHAR>());
#endif
}
template<class T>
std::string nformat(T value)
{
std::ostringstream ss;
ss.imbue(get_numpunct_locale());
ss << value;
return ss.str();
}