Skip to main content

APIzation: Replication Package

[Q#24753177][A#24753336] How to split a double number by dot into two decimal numbers in Java?

https://stackoverflow.com/q/24753177

Trying to split a double number into two decimal parts by dot. Like this: 1.9 into 1 and 9; 0.16 into 0 and 16; Here's what I do, but seems a little redundant, what's the best way to do this? The origin number will always be like Just 0.x or 1.x or 0.xx or 1.xx and xx > 10 My way of doing this seems using to much String conversion,which I don't think is very efficient.

Answer

https://stackoverflow.com/a/24753336

You can try this way too

APIzation

double val=1.9;
    String[] arr=String.valueOf(val).split("\\.");
    int[] intArr=new int[2];
    intArr[0]=Integer.parseInt(arr[0]); // 1
    intArr[1]=Integer.parseInt(arr[1]); // 9
package com.stackoverflow.api;

public class Human24753336 {

  public static int[] getDecimalSplitByDot(double val) {
    String[] arr = String.valueOf(val).split("\\.");
    int[] intArr = new int[2];
    intArr[0] = Integer.parseInt(arr[0]); // 1
    intArr[1] = Integer.parseInt(arr[1]); // 9
    return intArr;
  }
}

package com.stackoverflow.api;

/**
 * How to split a double number by dot into two decimal numbers in Java?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/24753336">https://stackoverflow.com/a/24753336</a>
 */
public class APIzator24753336 {

  public static void splitNumber(double val, int[] intArr)
    throws Exception {
    String[] arr = String.valueOf(val).split("\\.");
  }
}