Java将字符串转换为双精度

Java String to double conversion can be done by many ways. Today we will look into some common ways to convert java string to double primitive data type or Double object. Note that since java supports autoboxing, double primitive type and Double object can be used interchangeably without any issues.

Java String到Double的转换可以通过许多方法来完成。 今天,我们将研究将Java字符串转换为double原始数据类型或Double对象的一些常用方法。 请注意,由于Java支持自动装箱,因此double原始类型和Double对象可以互换使用而没有任何问题。

Double d1 = 10.25d;
//autoboxing from double to Double
double d = Double.valueOf(10.25); 
//unboxing from Double to double

Java将字符串转换为双精度 (Java Convert String to Double)

Let’s look at all the different ways to convert string to double in java.

让我们看看在Java中将字符串转换为double的所有不同方法。

  1. Double.parseDouble() (Double.parseDouble())

    We can parse String to double using parseDouble() method. String can start with “-” to denote negative number or “+” to denote positive number. Also any trailing 0s are removed from the double value. We can also have “d” as identifier that string is a double value. This method returns double primitive type. Below code snippet shows how to convert string to double using Double.parseDouble() method.

    String str = "+123.4500d";
    double d = Double.parseDouble(str); // returns double primitive
    System.out.println(d); //-123.45, trailing 0s are removed
    
    System.out.println(Double.parseDouble("123.45001")); //123.45001
    System.out.println(Double.parseDouble("123.45001d")); //123.45001
    System.out.println(Double.parseDouble("123.45000")); //123.45
    System.out.println(Double.parseDouble("123.45001D")); //123.45001

    我们可以使用parseDouble()方法将String解析为两倍。 字符串可以以“-”开头表示负数或以“ +”开头表示正数。 同样,从双精度值中删除所有尾随的0。 我们还可以将“ d”标识为字符串是双精度值。 此方法返回双基本类型。 下面的代码片段显示了如何使用Double.parseDouble()方法将字符串转换为double。

  2. Double.valueOf() (Double.valueOf())

    This method works almost similar as parseDouble() method, except that it returns Double object. Let’s see how to use this method to convert String to Double object.

    String str = "123.45";
    Double d = Double.valueOf(str); // returns Double object
    System.out.println(d); //123.45
    
    System.out.println(Double.valueOf("123.45d")); //123.45
    System.out.println(Double.valueOf("123.4500d")); //123.45
    System.out.println(Double.valueOf("123.45D")); //123.45

    此方法与parseDouble()方法几乎一样,不同之处在于它返回Double对象。 让我们看看如何使用此方法将String转换为Double对象。

  3. 新的Double(String s) (new Double(String s))

    We can convert String to Double object through it’s constructor too. Also if we want double primitive type, then we can use doubleValue() method on it. Note that this constructor has been deprecated in Java 9, preferred approach is to use parseDouble() or valueOf() methods.

    String str = "98.7";
    double d = new Double(str).doubleValue(); //constructor deprecated in java 9
    System.out.println(d); //98.7

    我们也可以通过其构造函数将String转换为Double对象。 另外,如果我们要使用双基元类型,则可以在其上使用doubleValue()方法。 请注意,此构造函数已在Java 9中弃用,首选方法是使用parseDouble()valueOf()方法。

  4. DecimalFormat parse() (DecimalFormat parse())

    This is useful to parse formatted string to double. For example, if String is “1,11,111.23d” then we can use DecimalFormat to parse this string to double as:

    String str = "1,11,111.23d";
    try {
    	double l = DecimalFormat.getNumberInstance().parse(str).doubleValue();
    	System.out.println(l); //111111.23
    } catch (ParseException e) {
    	e.printStackTrace();
    }

    Note that parse() method returns instance of Number, so we are calling doubleValue() to get the double primitive type from it. Also this method throw ParseException if the string is not properly formatted.

    这对于将格式化的字符串解析为两倍很有用。 例如,如果String为“ 1,11,111.23d”,则可以使用DecimalFormat将此字符串解析为以下形式的两倍:

    请注意,parse()方法返回Number实例,因此我们正在调用doubleValue()以从中获取double基本类型。 如果字符串的格式不正确,此方法ParseException引发ParseException

That’s all for converting string to double in java program.

这就是在java程序中将字符串转换为double的全部。

Reference: Double API Doc

参考: Double API文档

翻译自: https://www.journaldev.com/18392/java-convert-string-to-double

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值