Convert double to byte[] array

前端 未结 5 1470
灰色年华
灰色年华 2020-12-22 10:16

How can I convert double to byte array in Java? I looked at many other posts, but couldn\'t figure out the right way.

Input = 65.43 
byte[] size = 6
precisi         


        
5条回答
  •  醉梦人生
    2020-12-22 10:47

    public static byte[] encode(double input, int size, int precision) {
        double tempInput = input;
    
        for (int i = 0; i < precision; i++) tempInput *= 10;
    
        int output = (int) tempInput;
    
        String strOut = String.format("%0"+size+"d", output);
    
        return strOut.getBytes();
    }
    

提交回复
热议问题