java - 自定义法国数字格式

标签 java

我尝试为美国国家/地区自定义数字格式。到目前为止它工作正常。

    // Not something I want.
    NumberFormat numberFormat0 = NumberFormat.getNumberInstance(Locale.US);        
    System.out.println("US = " + numberFormat0.format(1234.0) + " (I wish to have 1,234.00)");
    System.out.println("US = " + numberFormat0.format(1234.567) + " (I wish to have 1,234.567)");
    System.out.println("US = " + numberFormat0.format(1234.5678) + " (I wish to have 1,234.568)\n");

    // Yes. Something I want :)
    NumberFormat goodNumberFormat = new DecimalFormat("#,##0.00#");
    System.out.println("US = " + goodNumberFormat.format(1234.0) + " (I wish to have 1,234.00)");
    System.out.println("US = " + goodNumberFormat.format(1234.567) + " (I wish to have 1,234.567)");
    System.out.println("US = " + goodNumberFormat.format(1234.5678) + " (I wish to have 1,234.568)\n");

这是输出。

US = 1,234 (I wish to have 1,234.00)
US = 1,234.567 (I wish to have 1,234.567)
US = 1,234.568 (I wish to have 1,234.568)

US = 1,234.00 (I wish to have 1,234.00)
US = 1,234.567 (I wish to have 1,234.567)
US = 1,234.568 (I wish to have 1,234.568)

但是,同样的事情在法国并不奏效。因为他们使用 , 表示 .,使用“空格”表示 ,.

我写了下面的代码。

    // Not something I want.
    NumberFormat numberFormat1 = NumberFormat.getNumberInstance(Locale.FRANCE);        
    System.out.println("FRANCE = " + numberFormat1.format(1234.0) + " (I wish to have 1 234,00)");
    System.out.println("FRANCE = " + numberFormat1.format(1234.567) + " (I wish to have 1 234,567)");
    System.out.println("FRANCE = " + numberFormat1.format(1234.5678) + " (I wish to have 1 234,567)\n");

    // Exception in thread "main" java.lang.IllegalArgumentException: Malformed pattern "# ##0,00#"
    NumberFormat goodNumberFormat1 = new DecimalFormat("# ##0,00#");
    System.out.println("FRANCE = " + goodNumberFormat1.format(1234.0) + " (I wish to have 1 234,00)");
    System.out.println("FRANCE = " + goodNumberFormat1.format(1234.567) + " (I wish to have 1 234,567)");
    System.out.println("FRANCE = " + goodNumberFormat1.format(1234.5678) + " (I wish to have 1 234,567)\n");

我收到以下错误。

FRANCE = 1 234 (I wish to have 1 234,00)
FRANCE = 1 234,567 (I wish to have 1 234,567)
FRANCE = 1 234,568 (I wish to have 1 234,567)
Exception in thread "main" java.lang.IllegalArgumentException: Malformed pattern "# ##0,00#"

我能做些什么来获得上述自定义数字格式吗?

最佳答案

    DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(Locale.FRANCE);
    NumberFormat goodNumberFormat = new DecimalFormat("#,##0.00#", dfs);

关于java - 自定义法国数字格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8389392/

相关文章:

java - Vertx 线程模型

java - 这会被定义为堆排序的一种形式吗?

java - 调用方法 JobRequest.Builder.schedule() 时出现 java.lang.IllegalStateException

java - Web 框架 : How is Play different from Spring MVC?

java - PrintStream 类型中的方法 println(boolean) 不适用于参数 (void)

java - 类作为 Android 应用程序的弹出窗口

java - 如何使用 selenium rc 和 java 自动更改动态值

java - 使用 DataBufferInt 更改 bufferedImage 中的亮度

java - 使用 SimonVT 数字选择器并且无法膨胀 xml

java - 防止 Cassandra 转储 hprof 文件