在 Java 中将 Double 转换为 Int

本教程介绍了如何在 Java 中将 double 转换为 int。

double 类型用于存储浮点数,integer 类型用于存储非小数(整数)值。有几种方法可以将 double 类型转换为整数,例如类型转换,double 类的 intValue() 方法。让我们看一些例子。

在 Java 中使用类型转换将 double 转换为 int

这是在 Java 中将 double 转换为 int 的最简单方法。在这里,我们使用类型转换来获取整数结果。很好,但是它会截断实际值。它仅返回整数部分,不包括小数点。请参见下面的示例。

public class SimpleTesting{
    public static void main(String[] args) {
        double d_val = 12.90;
        System.out.println("Value in double: "+ d_val);
        int i_val = (int) d_val;
        System.out.println("Value in int: "+i_val);
    }
}

输出:

Value in double: 12.9
Value in int: 12

在 Java 中使用 round() 方法将 double 转换为 int

我们可以使用 Mathround() 方法将双精度型转换为整数类型。我们使用 round() 方法,因为它会将值四舍五入为最接近的整数。它有助于减少数据截断。请参见下面的示例。

public class SimpleTesting{
    public static void main(String[] args) {
        double d_val = 12.90;
        System.out.println("Value in double: "+ d_val);
        int i_val = (int) Math.round(d_val);
        System.out.println("Value in int: "+i_val);
    }
}

输出:

Value in double: 12.9
Value in int: 13

如你所见,在上面的示例中,强制转换返回 12,而在此示例中,强制转换返回 13,因为 round() 方法返回了舍入值。

使用 Java 中的 intValue() 方法将 double 转换为 int

Double 是 Java 中的包装类,它具有一种 intValue() 方法,该方法从 double 值中返回一个整数。这很容易,因为它是一个内置方法,因此我们不需要使用任何其他类,而只需使用该方法即可获得结果。请参见下面的示例。

public class SimpleTesting{
    public static void main(String[] args) {
        Double d_val = 12.90; // store into wrapper
        System.out.println("Value in double: "+ d_val);
        int i_val = d_val.intValue();
        System.out.println("Value in int: "+i_val);
    }
}

输出:

Value in double: 12.9
Value in int: 12

在 Java 中将 Double 转换为字符串

本教程介绍如何在 Java 中将 double 类型转换为 string 对象。我们还列出了一些示例代码,以帮助你更好地理解该主题。

术语 double 是 Java 中的一种数据类型,用于存储实数或浮点数。另一方面,字符串是包含用双引号括起来的字符序列的类。

这篇文章教你如何使用一些内置方法将双精度对象转换为字符串对象,例如 valueOf()toString()valueOf()。让我们开始吧!

在 Java 中将 Double 转换为字符串

第一种方法是一个简单的例子,我们有一个 double 类型的可变价格,它包含一个浮点值。使用字符串的 valueOf() 方法,我们将此值转换为 string 对象。请参考下面的示例。

public class Main
{
    public static void main(String[] args)
    {
        double price = 44.22;
        String val = String.valueOf(price);
        System.out.println(val);
    }
}

输出:

44.22

使用 Java 中的 toString() 方法将 Double 转换为字符串

Java Double 包装器类提供了一个 toString() 方法,该方法采用 double 类型参数并返回一个字符串对象。我们可以使用这个方法在 Java 中将 double 类型转换为字符串对象。请参考下面的示例。

public class Main
{
    public static void main(String[] args)
    {
        double price = 44.22;
        String val = Double.toString(price);
        System.out.println(val);
    }
}

输出:

44.22

使用 Java 中的 format() 方法将 Double 转换为字符串

Java 具有属于 string 类的 format() 方法,用于格式化字符串对象;我们可以使用这个过程将双精度值转换为字符串对象。

它需要两个参数:第一个是值格式,或者你最好说格式说明符,第二个是我们想要转换的值。请参考下面的示例。

public class Main
{
    public static void main(String[] args)
    {
        double price = 44.22;
        String val = String.format ("%.2f", price);
        System.out.println(val);
    }
}

输出:

44.22

使用 Java 中的 Decimalformat 类将 Double 转换为字符串

Java DecimalFormat 类用于格式化十进制数据。我们将使用此类的 format() 方法将双精度数据类型转换为 Java 中的字符串。请参考下面的示例。

import java.text.DecimalFormat;
public class Main
{
    public static void main(String[] args)
    {
        double price = 44.22;
        DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
        String val = decimalFormat.format(price);
        System.out.println(val);
    }
}

输出:

44.22

在 Java 中使用 + 运算符将 Double 转换为字符串

这是在 Java 中将任何数据类型转换为字符串的最简单方法之一。在这里,如果我们将任何值连接到字符串,结果将是字符串类型。因此,我们将在这里使用相同的概念在 Java 中将 double 类型转换为字符串。请参考下面的示例。

public class Main
{
    public static void main(String[] args)
    {
        double price = 44.22;
        String val = price+"";
        System.out.println(val);
    }
}

输出:

44.22