如何在Matlab中打印带有千位分隔符的整数? [英] How to print an integer with a thousands separator in Matlab?

查看:259
本文介绍了如何在Matlab中打印带有千位分隔符的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用逗号作为千位分隔符将数字转换成字符串.像这样:

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

但有效果

str = '120,501,231.21' 

如果内置的fprintf/sprintf无法做到这一点,我想可以使用正则表达式(可能通过调用Java(我假设它具有一些基于语言环境的格式化程序))或使用基本的字符串插入操作.但是,我既不是Matlab regexp的专家,也不是从Matlab调用Java的专家.

If the built-in fprintf/sprintf can't do it, I imagine cool solution could be made using regular expressions, perhaps by calling Java (which I assume has some locale-based formatter), or with a basic string-insertion operation. However, I'm not an expert in either Matlab regexp's or calling Java from Matlab.

相关问题: 如何我可以在 Python 中打印带有数千个分隔符的浮点数吗?

Related question: How can I print a float with thousands separators in Python?

Matlab 中是否有确定的方法可以做到这一点?

Is there any established way to do this in Matlab?

推荐答案

使用数千个分隔符设置数字格式的一种方法是调用Java语言环境感知格式器. 未记录的Matlab"博客上的"格式化数字"文章介绍了如何执行此操作:

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     

其中char(…)将Java字符串转换为Matlab字符串.

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

瞧瞧!

这篇关于如何在Matlab中打印带有千位分隔符的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆