What is the best way to separate double into two parts “integer & fraction” in java

前端 未结 5 969
礼貌的吻别
礼貌的吻别 2021-02-14 00:28

I have tried to separate 5.6 (for example) by the following method:

private static double[] method(double d)
{
    int integerPart = 0;
    double fractionPart =         


        
5条回答
  •  孤独总比滥情好
    2021-02-14 01:00

    String doubleAsString = Double.toString(123.456);

    String beforeDecimal=doubleAsString.substring(0,doubleAsString.indexOf(".")); //123

    String afterDecimal=doubleAsString.substring(doubleAsString.indexOf(".")+1); //456

提交回复
热议问题