1. 程式人生 > >c++ 正則實現 千分位分割

c++ 正則實現 千分位分割

stream sstream clas pla bsp bre att nbsp class

 1 #include <iostream>
 2 #include <string>
 3 #include <sstream>
 4 #include <regex>
 5 
 6 std::string ToThousandsNum(int num)
 7 {
 8     std::stringstream ss;
 9     ss << num;
10 
11     std::string str = ss.str();
12     std::regex pattern("^(-?\\d+)(\\d{3})
"); 13 14 while (true) 15 { 16 str = std::regex_replace(str, pattern, "$1,$2"); 17 if (!std::regex_search(str, pattern)) 18 break; 19 } 20 21 return str; 22 }

c++ 正則實現 千分位分割