Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的美元货币格式_Java_String_Currency - Fatal编程技术网

Java中的美元货币格式

Java中的美元货币格式,java,string,currency,Java,String,Currency,在Java中,如何有效地将像1234.56这样的浮点数和类似的大小数转换成像$1234.56 我正在寻找以下信息: 字符串12345.67变为字符串$12345.67 我还希望用Float和bigdecimic来实现这一点。有一个对区域设置敏感的习惯用法,效果很好: DecimalFormat moneyFormat = new DecimalFormat("$0.00"); System.out.println(moneyFormat.format(1234.56)); import jav

在Java中,如何有效地将像
1234.56
这样的浮点数和类似的大小数转换成像
$1234.56

我正在寻找以下信息:

字符串
12345.67
变为字符串
$12345.67


我还希望用
Float
bigdecimic
来实现这一点。

有一个对区域设置敏感的习惯用法,效果很好:

DecimalFormat moneyFormat = new DecimalFormat("$0.00");
System.out.println(moneyFormat.format(1234.56));
import java.text.NumberFormat;

// Get a currency formatter for the current locale.
NumberFormat fmt = NumberFormat.getCurrencyInstance();
System.out.println(fmt.format(120.00));
如果您当前的地区在美国,
println
将打印$120.00

另一个例子:

import java.text.NumberFormat;
import java.util.Locale;

Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
System.out.println(fmt.format(120.00));

这将打印:£120.00

这是根据您的输入和输出的代码::

该程序的输出是$12345.67,用于BigDecimal和number,也用于float

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class test {
    public static void main(String[] args) {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setGroupingSeparator(',');
        String pattern = "$#,##0.###";
        DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
        BigDecimal bigDecimal = new BigDecimal("12345.67");

        String bigDecimalConvertedValue = decimalFormat.format(bigDecimal);
        String convertedValue = decimalFormat.format(12345.67);

        System.out.println(bigDecimalConvertedValue);
        System.out.println(convertedValue);
    }
}

杰出的我喜欢这个解决方案