Converting String to Number in Java

天大地大妈咪最大 提交于 2020-06-08 05:26:05

问题


How can i convert a string to a abstract number, provided string is any valid number in java (say int, long, double etc).

I will not know the type of number in the string, so i can't use specific primitive parsing (like Integer.parseInt, Long.parseLong etc). Is there any generic way to convert it?

Eg:

  • String -> Number
  • "23423" -> 23423
  • "34.3" -> 34.3
  • "45364573747546" -> 45364573747546

回答1:


Use NumberFormat. Number cannot be instantiated because it is an abstract class.

 Number number = NumberFormat.getInstance().parse(string);



回答2:


Two cases:

  1. If you input string is less than 8 Bytes:

double primitiveNumber = Double.parseDouble(input);

  1. If Your input string is more than 8 bytes: you anyway cannot store it in a primitive, Then you can go with Number class, but this is not likely to happen since you expect a primitive.



回答3:


Double will make your value lose precision if too long.

You should use a BigDecimal instead:

BigDecimal number = new BigDecimal("43.256");

You can then get different primitives like this:

try {
  int intValue = number.intValueExact();
} catch (ArithmeticException e) {
  try {
    long longValue = number.longValueExact();
  } catch (ArithmeticException e) {
    double doubleValue = number.doubleValue();
  }
}



回答4:


Double.valueOf() will do fine unless you have a very long number.




回答5:


The quickest way to do this is just always use Double.parseDouble(). If you need a general-purpose parser that determines which primitive to use, rather than always using the same type, then you will need to write your own. One way to do this is to use each parseXxx() method and lot of try...catch statements.




回答6:


For integers:

int myInteger = Integer.parseInt("23423");

For doubles:

double myDouble = Double.parseDouble("34.3");



回答7:


As long as your strings could be big numbers (suppose that no longer than primitive long and double), then generic way could be decide whether it is long or double.

Number n = Double.valueOf(string);
if((double)n.intValue() == n.doubleValue()){
   //n is double use n.doubleValue() or just use the n for generic purposes
}
else{
   //n is int use n.intValue() or just use the n for generic purposes
}



回答8:


I do prefer BigDecimal only. Because it won't have the issues with size and precision




回答9:


You can use Double.parseDouble() to convert your string to double. This will work even if the string is an integer:

String myString = "1234.56";
double myDouble = Double.parseDouble(myString);

If you need to check if it is an integer or a double:

String myString = "1234.56";
int myInt;
double myDouble = Double.parseDouble(myString);
if ((myDouble % 1) == 0) {
   myInt = (int) myDouble;
   System.out.println("myString is an integer: " + myInt );       
} else {
   System.out.println("myString is a double: " + myDouble );    
}

Or you can use Guava's DoubleMath.isMathematicalInteger():

String myString = "1234.56";
int myInt;
double myDouble = Double.parseDouble(myString);
if (DoubleMath.isMathematicalInteger(myDouble)) {
   myInt = (int) myDouble;
   System.out.println("myString is an integer: " + myInt );       
} else {
   System.out.println("myString is a double: " + myDouble );    
}


来源:https://stackoverflow.com/questions/41813533/converting-string-to-number-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!