Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在Matlab中打印带有千位分隔符的整数?_Regex_Matlab - Fatal编程技术网

Regex 如何在Matlab中打印带有千位分隔符的整数?

Regex 如何在Matlab中打印带有千位分隔符的整数?,regex,matlab,Regex,Matlab,我想使用逗号作为千位分隔符将数字转换成字符串。比如: x = 120501231.21; str = sprintf('%0.0f', x); 但实际上 str = '120,501,231.21' 如果内置的fprintf/sprintf做不到这一点,我想可以使用正则表达式,或者通过调用Java(我假设它有一些基于区域设置的格式化程序)或者使用基本的字符串插入操作,来创建一个很酷的解决方案。然而,我不是MatlabRegexp或从Matlab调用Java方面的专家 相关问题: 在Matl

我想使用逗号作为千位分隔符将数字转换成字符串。比如:

x = 120501231.21;
str = sprintf('%0.0f', x);
但实际上

str = '120,501,231.21' 
如果内置的
fprintf
/
sprintf
做不到这一点,我想可以使用正则表达式,或者通过调用Java(我假设它有一些基于区域设置的格式化程序)或者使用基本的字符串插入操作,来创建一个很酷的解决方案。然而,我不是MatlabRegexp或从Matlab调用Java方面的专家

相关问题:


在Matlab中有没有现成的方法来实现这一点?

使用数千个分隔符格式化数字的一种方法是调用Java语言环境感知格式化程序。“Undocumented Matlab”博客上的“”文章解释了如何做到这一点:

>> nf = java.text.DecimalFormat;
>> str = char(nf.format(1234567.890123))

str =

1,234,567.89     
其中,
char(…)
将Java字符串转换为Matlab字符串


下面是使用正则表达式的解决方案:

%# 1. create your formated string 
x = 12345678;
str = sprintf('%.4f',x)

str =
12345678.0000

%# 2. use regexprep to add commas
%#    flip the string to start counting from the back
%#    and make use of the fact that Matlab regexp don't overlap
%#    The three parts of the regex are
%#    (\d+\.)? - looks for any number of digits followed by a dot
%#               before starting the match (or nothing at all)
%#    (\d{3})  - a packet of three digits that we want to match
%#    (?=\S+)   - requires that theres at least one non-whitespace character
%#               after the match to avoid results like ",123.00"

str = fliplr(regexprep(fliplr(str), '(\d+\.)?(\d{3})(?=\S+)', '$1$2,'))

str =
12,345,678.0000

这是可行的,但有点麻烦。我相信还有其他方法会很有趣/有用。@NasserM.Abbasi:很好的发现-但是如果有简单的正则表达式,这是一个多么复杂的方法:)不,仍然不适用于
120501231.890123
。我冒昧地为您修复了它。@EitanT:btw:我修复了括号的拼写错误,并添加了一些正则表达式的解释。很好的团队合作:)+1:很好。也许值得一提的是,这在八度音阶下不起作用。在多语言系统上呢?我想使用EN格式。我有什么办法可以选择吗?