注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Mr.Right

不顾一切的去想,于是我们有了梦想。脚踏实地的去做,于是梦想成了现实。

 
 
 

日志

 
 
关于我

人生一年又一年,只要每年都有所积累,有所成长,都有那么一次自己认为满意的花开时刻就好。即使一时不顺,也要敞开胸怀。生命的荣枯并不是简单的重复,一时的得失不是成败的尺度。花开不是荣耀,而是一个美丽的结束,花谢也不是耻辱,而是一个低调的开始。

网易考拉推荐

Matlab空格和逗号作为千位分隔符demo  

2014-08-14 19:42:04|  分类: 编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
1)采用Java中的方法
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.
% 使用空格作为分隔符
dfs = java.text.DecimalFormatSymbols(java.util.Locale('zh'));  % ChinaLocale
nf = java.text.DecimalFormat;
nf.setMaximumFractionDigits(1);
% dfs.setGroupingSeparator(''''); % use ' rather than , dfs.setGroupingSeparator(' '); % use space rather than , nf.setDecimalFormatSymbols(dfs) str1 = char(nf.format(123456.789012))

2)采用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"

str1 = fliplr(regexprep(fliplr(str), '(\d+\.)?(\d{3})(?=\S+)', '$1$2,')) % 采用逗号作为千位分隔符
str2 = fliplr(regexprep(fliplr(str), '(\d+\.)?(\d{3})(?=\S+)', '$1$2 '))   % 采用空格作为千位分隔符

——————————————————————————————————————————

In the simple code snippet above, note the two alternative manners in which I set the DecimalFormat object’s properties, once with theset(‘XYZ’,value) function, then using the corresponding Java accessor method setXYZ(value). In most cases, we find that Java properties can be set using either of these manners, although using the Java accessor methods is typically safer and prevents some possible problems. Similarly, property values can be retrieved using either get(‘XYZ’) or the corresponding getXYZ() or isXYZ() Java methods (which are better).

Some of the interesting gettable/settable properties of the DecimalFormat object include (my Locale’s defaults are specified – your Locale might be different):

  • Currency – a java.util.Currency object that sets the currency symbol, international currency code and number of fractional decimal digits
  • DecimalFormatSymbols – a java.text.DecimalFormatSymbols object that sets the character symbols for such things as the decimal sign (“.” or “,” for example), grouping separator (“,” or ” ‘ ” or whatever), percent and per-mill signs, minus sign, infinity sign etc.
  • DecimalSeparatorAlwaysShown – (default=false) a boolean flag that specifies whether or not to display the decimal sign even for integer numbers. For example, “123.” rather than “123″.
  • GroupingSize – (default=3) indicates how many integer digits should be grouped by the grouping (“thousands”) separator sign.
  • GroupingUsed – (default=true) a boolean flag that specifies whether or not to use the grouping sign at all. Matlab’s built-innum2str does not use grouping.
  • MaximumFractionDigits – (default=3) the maximal number of digits to display to the right of the decimal sign. Post-rounding trailing zeros are omitted.
  • MinimumFractionDigits – (default=0) the minimum number of digits to display, padding with trailing zeros if necessary.
  • MaximumIntegerDigits – (default=intmax) the maximal number of digits to display to the left of the decimal sign. Extra digits are removed.
  • MinimumIntegerDigits – (default=1) the minimum number of digits to display, padding with leading zeros if necessary.
  • Multiplier – (default=1) multiplies the number by the specified value before converting to string.
  • NegativePrefix – (default=’-') the string to display to the left of a negative number.
  • NegativeSuffix – default = ”) the string to display to the right of a negative number. Some bankers like to display negative numbers as “(value)” and this is easy to do using NegativePrefix and NegativeSuffix. There are corresponding propertiesPositivePrefix and PositiveSuffix.
  • RoundingMode – (default=RoundingMode.HALF_EVEN) a java.math.RoundingMode object that sets the manner in which numbers are rounded to fit the required number of digits.
value = nf.parse('123''456.789').doubleValue

参考文献:http://undocumentedmatlab.com/blog/formatting-numbers
  评论这张
 
阅读(1224)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016