Today we will see different ways to convert double to string in java. Java double to string conversion can be done in many ways, we will go through them one by one with example code snippets.

今天,我们将看到在Java中将double转换为string的不同方法。 Java从双串到字符串的转换可以通过多种方式完成,我们将通过示例代码片段逐一介绍它们。

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

java convert double to string, java double to string

Let’s look at different code snippets for java double to string conversion. Note that double is a primitive data type whereas Double is an Object. However java supports autoboxing, so they both can be used interchangeably in most of the cases.

让我们看一下Java双重字符串转换的不同代码段。 请注意, double是原始数据类型,而Double是对象。 但是java支持autoboxing ,因此它们在大多数情况下都可以互换使用。

  1. 使用+运算符 (Using + operator)

    This is the easiest way to convert double to string in java.

    double d = 123.45d;
    String str = d+""; // str is '123.45'


  2. Double.toString() (Double.toString())

    We can use Double class toString method to get the string representation of double in decimal points. Below code snippet shows you how to use it to convert double to string in java.

    double d = 123.45d;
    String str = Double.toString(d);
    System.out.println(str); //prints '123.45'

    我们可以使用Double类toString方法获取小数点中double的字符串表示形式。 下面的代码段显示了如何在Java中使用它将double转换为string

  3. String.valueOf() (String.valueOf())

    double d = 123.456d;
    String str = String.valueOf(d); // str is '123.456'

  4. 新Double(double l) (new Double(double l))

    Double constructor with double argument has been deprecated in Java 9, but you should know it.

    double d = 123.45d;
    //deprecated from Java 9, use valueOf for better performance
    String str = new Double(d).toString();

    Java 9中已弃用带双参数的Double构造函数,但是您应该知道这一点。

  5. String.format() (String.format())

    We can use Java String format method to convert double to String in our programs.

    double d = 36.98d;
    String s = String.format("%f", d);
    System.out.println(s); //36.980000

    我们可以使用Java String格式方法在程序中将double转换为String。

  6. 小数格式 (DecimalFormat)

    We can use DecimalFormat class to convert double to String. We can also get string representation with specified decimal places and rounding of half-up.

    double d = 123.454d;
    String str = DecimalFormat.getNumberInstance().format(d);
    System.out.println(str); //str is '123.454'
    //if you don't want formatting
    str = new DecimalFormat("#.0#").format(d); // rounded to 2 decimal places
    System.out.println(str); //str is '123.45'
    str = new DecimalFormat("#.0#").format(123.456); // rounded to 2 decimal places
    System.out.println(str); //str is '123.46'

    我们可以使用DecimalFormat类将double转换为String。 我们还可以使用指定的小数位数和四舍五入取整来获得字符串表示形式。

  7. StringBuilder,StringBuffer (StringBuilder, StringBuffer)

    We can use StringBuilder and StringBuffer append function to convert double to string.

    double d = 123.45d;
    String str = new StringBuilder().append(d).toString();

    我们可以使用StringBuilderStringBuffer append函数将double转换为string。

Java Double to String示例 (Java Double to String Example)

Here is a simple program where we will convert double to string and print it using all the different methods we saw above.


package com.journaldev.string;

import java.text.DecimalFormat;

public class JavaDoubleToString {

	public static void main(String[] args) {
		double d = 123.45d;
		String str = Double.toString(d);

		str = String.valueOf(d);

		// deprecated from Java 9, use valueOf for better performance
		str = new Double(d).toString();

		str = String.format("%f", d);
		System.out.println(str); //123.450000

		str = d + "";

		str = DecimalFormat.getNumberInstance().format(d);

		str = new DecimalFormat("#.0#").format(d);

		str = new StringBuilder().append(d).toString();


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


Reference: Double API Doc

参考: Double API文档


评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页