Convert String to double in Java

后端 未结 14 1653
傲寒
傲寒 2020-11-22 05:52

How can I convert a String such as \"12.34\" to a double in Java?

14条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 06:13

    Use new BigDecimal(string). This will guarantee proper calculation later.

    As a rule of thumb - always use BigDecimal for sensitive calculations like money.

    Example:

    String doubleAsString = "23.23";
    BigDecimal price = new BigDecimal(doubleAsString);
    BigDecimal total = price.plus(anotherPrice);
    

提交回复
热议问题