How to print an integer with a thousands separator in Matlab?

后端 未结 2 1135
轮回少年
轮回少年 2021-02-20 00:08

I would like to turn a number into a string using a comma as a thousands separator. Something like:

x = 120501231.21;
str = sprintf(\'%0.0f\', x);
2条回答
  •  孤城傲影
    2021-02-20 00:28

    One way to format numbers with thousands separators is to call the Java locale-aware formatter. The "formatting numbers" article at the "Undocumented Matlab" blog explains how to do this:

    >> nf = java.text.DecimalFormat;
    >> str = char(nf.format(1234567.890123))
    
    str =
    
    1,234,567.89     
    

    where the char(…) converts the Java string to a Matlab string.

    voilà!

提交回复
热议问题