Wrong Answer with Input: "123456789", "987654321"


  • 0
    R
    import java.text.DecimalFormat;
    
    public class Solution {
        public String multiply(String num1, String num2) {
            
            double x1 = Double.parseDouble(num1);
            double x2 = Double.parseDouble(num2);
            
            DecimalFormat df = new DecimalFormat("#");
            df.setMaximumFractionDigits(0);
            return df.format(x1 * x2) + "";
            
        }
    }
    

    Input: "123456789", "987654321"
    Output: "121932631112635264"
    Expected: "121932631112635269"

    why? can anybody help explain? thx


  • 0
    H

    Cause the significant digits for double is 15 (actually 16 for numbers smaller than 4503599627370496)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.