开发者

ICU Custom Currency Formatting (C++)

开发者 https://www.devze.com 2022-12-14 09:19 出处:网络
Is it possible to custom format currency strings using the ICU library similar to the way it lets you format time string开发者_运维问答s by providing a format string (e.g. \"mm/dd/yyy\").

Is it possible to custom format currency strings using the ICU library similar to the way it lets you format time string开发者_运维问答s by providing a format string (e.g. "mm/dd/yyy").

So that for a given locale (say USD), if I wanted I could have all currency strings come back "xxx.00 $ USD".


See http://icu-project.org/apiref/icu4c/classDecimalFormat.html,

Specifically: http://icu-project.org/apiref/icu4c/classDecimalFormat.html#aadc21eab2ef6252f25eada5440e3c65

For pattern syntax see: http://icu-project.org/apiref/icu4c/classDecimalFormat.html#_details

I didn't used this but from my knowledge of ICU this is the direction.

However I would suggest to use:

http://icu-project.org/apiref/icu4c/classNumberFormat.html and createCurrencyInstance member and then use setMaximumIngegerDigits or other functions to make what you need -- that would be much more localized. Try not assume anything about any culture. Because "10,000 USD" my be misinterpreted as "$ 10" in some countries where "," used for fraction part separation.

So be careful.


You can create a currency instance, then if it is safe to cast it to a DecimalFormat

if (((const NumberFormat*)fmt)->getDynamicClassID() == DecimalFormat::getStaticClassID()) 
  {   const DecimalFormat* df = (const DecimalFormat*) fmt; ...

… then you can call applyPattern on it. See the information on ¤, ¤¤, ¤¤¤ under 'special pattern chars'


Use the ICU library's createCurrencyInstance().

0

精彩评论

暂无评论...
验证码 换一张
取 消