You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:如何让Double值格式化后显示小数点而非逗号?

Fixing Decimal Comma Instead of Dot in TextView

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:01:59