简体   繁体   English

在Java中将字符串转换为数字

[英]Converting String to Number in Java

How can i convert a string to a abstract number, provided string is any valid number in java (say int, long, double etc). 如果字符串是java中的任何有效数字(比如int,long,double等),我如何将字符串转换为抽象数字。

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). 我不会知道字符串中的数字类型,所以我不能使用特定的原始解析(如Integer.parseInt,Long.parseLong等)。 Is there any generic way to convert it? 有没有通用的方法来转换它?

Eg: 例如:

  • String -> Number 字符串 - >数字
  • "23423" -> 23423 “23423” - > 23423
  • "34.3" -> 34.3 “34.3” - > 34.3
  • "45364573747546" -> 45364573747546 “45364573747546” - > 45364573747546

Use NumberFormat. 使用NumberFormat。 Number cannot be instantiated because it is an abstract class. Number无法实例化,因为它是一个抽象类。

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

Double will make your value lose precision if too long. 如果时间过长, Double将使您的价值失去精确度。

You should use a BigDecimal instead: 您应该使用BigDecimal

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();
  }
}

Two cases: 两种情况:

  1. If you input string is less than 8 Bytes: 如果输入字符串小于8字节:

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. 如果您的输入字符串超过8个字节:您无论如何都无法将其存储在基元中,那么您可以使用Number类,但由于您需要基元,因此不太可能发生这种情况。

Double.valueOf()会很好,除非你有一个很长的数字。

The quickest way to do this is just always use Double.parseDouble() . 最快的方法是始终使用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. 一种方法是使用每个parseXxx()方法和许多try...catch语句。

For integers: 对于整数:

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

For doubles: 对于双打:

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

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. 只要你的字符串可以是大数字(假设不超过原始的long和double),那么通用方法可以决定它是long还是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
}

I do prefer BigDecimal only. 我更喜欢BigDecimal Because it won't have the issues with size and precision 因为它不会有尺寸和精度问题

You can use Double.parseDouble() to convert your string to double. 您可以使用Double.parseDouble()将字符串转换为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() : 或者您可以使用Guava的 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 );    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM