java - DecimalFormat 模式问题

标签 java android

我尝试在 Android 中使用 NumberFormatter 格式化数字。我使用下面的代码,它工作得很好:

 NumberFormat formatter = new DecimalFormat("###,###");
 String myFormattedString = formatter.format(123456);

但是,当我使用带有空格的模式时,如下所示:new DecimalFormat("###,# ##"); 它会抛出 IllegalArgumentException。我已阅读有关 NumberFormatter 和 DecimalFormatter 的文档,但没有发现有关模式中空格的信息。谁能解释一下为什么我不能使用空格或如何将它们添加为允许的字符。 提前致谢!!!

最佳答案

您不能在数字中间放置空格:这不是有效的格式。

如果你看the JavaDoc of DecimalFormat ,你会看到这个:

Prefix:
        any Unicode characters except \uFFFE, \uFFFF, and special characters  
Suffix:  
        any Unicode characters except \uFFFE, \uFFFF, and special characters  
Number:  
        Integer Exponentopt  
        Integer . Fraction Exponentopt

如果不复制整个文档,Number 模式的任何组件都不会接受空格,因此尝试在中间添加空格将不起作用。您只能在前缀或后缀中使用空格。

关于java - DecimalFormat 模式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6689621/

相关文章:

java - SonarQube 5.6 安卓 Gradle

java - 迭代多个 SortedSet 对象

java - 计算用户给定的字符串中的唯一字符

java - 在 Serializable Java 类中使用 Logger 的正确方法是什么?

java.sql.异常: attempt to write on read-only database in java Swing Application

android - IndexOutOfBoundsException 但错误没有给我代码中的一行

java - 在 Android 上使用 SQLite 时出现 OutOfMemory 异常

java - 无法使用 java.io.IOException : setDataSourceFD failed. : status=0x80000000 in 2. 3.3 android 平板电脑创建媒体播放器

android - 在Android Studio中切换 Activity 时不断出现错误

android - ListView Android 中的隐藏字段?