Parsing string to double - java

左心房为你撑大大i 提交于 2019-12-04 17:12:00

If your string representing double uses space ' ' instead of a decimal point, you can fix it by replacing the space with a dot, like this:

String currentBalanceStr = "891692 06";
double currentBalanceDbl = Double.parseDouble(currentBalanceStr.replaceAll(" ","."));

Link to ideone.

Using Double for balance calculation is not a good idea, it makes more sense to use the BigDecimal because it is more precise.

You can also add the currency using the class bellow:

Locale enUSLocale =
    new Locale.Builder().setLanguage("en").setRegion("US").build();

Currency currencyInstance = Currency.getInstance(enUSLocale);

System.out.println(
    "Symbol for US Dollar, en-US locale: " +
    currencyInstance.getSymbol(enUSLocale));

please go through this link for more details.

It seems like you are doing some currency calculation, so always use BigDecimal

BigDecimal balStr = new BigDecimal(String.replaceAll(" ",".");
String s="891692 06";
Double.parseDouble(s.replace(" ", ".")); would do the trick

you can use NumberFormatClass from the API to get the monetorey information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!