迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 中将浮点数转换为字符串以及将字符串转换为浮点数

作者:迹忆客 最近更新:2023/03/26 浏览次数:

本篇文章介绍了如何在 Java 中将浮点数转换为字符串和将字符串转换为浮点数。


使用 valueOf() 方法将字符串转换为浮点数

我们可以使用 Float 类的 valueOf() 方法将字符串转换为 Java 中的浮点数。valueOf() 方法采用一个参数并返回一个浮点型值。请参见下面的示例。

public class SimpleTesting{
    public static void main(String[] args) {
        String str = "123";
        System.out.println("String value: "+str);
        float f_Val = Float.valueOf(str);
        System.out.println("Float value: "+f_Val);
    }
}

输出:

String value: 123
Float value: 123.0

使用 parseFloat() 方法将字符串转换为浮点数

Float 类包含一个 parseFloat() 方法,该方法将字符串类型的值解析为浮点类型。它接受一个参数并返回一个浮点数。请参见以下示例。

public class SimpleTesting{
    public static void main(String[] args) {
        String str = "123";
        System.out.println("String value: "+str);
        float f_Val = Float.parseFloat(str);
        System.out.println("Float value: "+f_Val);
    }
}

输出:

String value: 123
Float value: 123.0

使用 Float() 方法将字符串转换为浮点数

在此示例中,我们使用 Float() 构造函数,该构造函数接受字符串类型的参数并返回原始类型的浮点数。我们可以使用它在 Java 中将字符串转换为浮点数。请参见以下示例。

public class SimpleTesting{
    public static void main(String[] args) {
        String str = "123";
        System.out.println("String value: "+str);
        float f_Val = new Float(str);
        System.out.println("Float value: "+f_Val);
    }
}

输出:

String value: 123
Float value: 123.0

使用 toString() 方法将浮点数转换为字符串

在这里,我们使用了 Float 类的 toString() 方法来获取浮点数的字符串类型。请参见下面的示例。

public class SimpleTesting{
    public static void main(String[] args) {
        float fVal = 23.25f;
        System.out.println("Float Value: "+fVal);
        String str = Float.toString(fVal);
        System.out.println("String Value: "+str);
    }
}

输出:

Float Value: 23.25
String Value: 23.25

使用+ 运算符将浮点数转换为字符串

在 Java 中,加号运算符可用于将浮点数转换为字符串。加号运算符用于将任何类型的值连接到字符串并返回字符串。请参见下面的示例。

public class SimpleTesting{
    public static void main(String[] args) {
        float fVal = 23.25f;
        System.out.println("Float Value: "+fVal);
        String str = ""+fVal;
        System.out.println("String Value: "+str);
    }
}

输出:

Float Value: 23.25
String Value: 23.25

使用 valueOf() 方法将浮点数转换为字符串

为了将浮点数转换为字符串,我们使用了 String 类的 valueOf() 方法,该方法接受一个 float 类型的参数并将一个字符串返回给调用者。请参见下面的示例。

public class SimpleTesting{
    public static void main(String[] args) {
        float fVal = 23.25f;
        System.out.println("Float Value: "+fVal);
        String str = String.valueOf(fVal);
        System.out.println("String Value: "+str);
    }
}

输出:

Float Value: 23.25
String Value: 23.25

使用 format() 方法将浮点数转换为字符串

当我们要获取指定格式的格式化字符串(例如小数点后两位)时,这很有用。因此,我们可以使用 DecimalFormat 类及其 format() 方法来获取字符串对象。请参见下面的示例。

import java.text.DecimalFormat;
public class SimpleTesting{
    public static void main(String[] args) {
        float fVal = 23.25f;
        System.out.println("Float Value: "+fVal);
        String str = new DecimalFormat("#.00").format (fVal);
        System.out.println("String Value: "+str);
    }
}

输出:

Float Value: 23.25
String Value: 23.25

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便