Android开发:如何让Double值格式化后显示小数点而非逗号?
Hey there! The problem you're facing happens because DecimalFormat relies on your device's default locale by default. In many regions, the locale uses a comma as the decimal separator instead of a dot. Here are two simple ways to force the dot separator you need:
Method 1: Use a Locale that Uses Dot as Decimal Separator
You can explicitly set a locale (like Locale.US) which uses dots for decimals when initializing DecimalFormat:
DecimalFormat precision = new DecimalFormat("0.00", DecimalFormatSymbols.getInstance(Locale.US)); final TextView totalPrice = (TextView) getView().findViewById(R.id.actualPrice); double price = 200.12357123; totalPrice.setText("$" + precision.format(price));
Method 2: Manually Set Decimal Separator
If you don't want to tie it to a specific locale, you can directly configure the DecimalFormatSymbols to use a dot:
DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setDecimalSeparator('.'); // Set the decimal separator to dot DecimalFormat precision = new DecimalFormat("0.00", symbols); final TextView totalPrice = (TextView) getView().findViewById(R.id.actualPrice); double price = 200.12357123; totalPrice.setText("$" + precision.format(price));
Either approach will make your TextView display $200.12 instead of $200,12, so your subsequent substring extraction and conversion to double will work as expected.
内容的提问来源于stack exchange,提问作者user9240225

