Java String To Float Examples

Java String To Float Examples

Feb 21, 2015 Core JavaExamplesSnippet comments

This tutorial will show example codes on how to convert Java String To Float. Performing conversion from String to float is a common scenario when programming with Core Java. 

Here are some quick reference example codes:

 

 

Convert using Float.parseFloat()

The Float.parseFloat() static method parses the string argument and returns a float value.

 

Syntax 

public static float parseFloat(String s) throws NumberFormatException

The parameter s will be converted to a primitive float value. Note that the method will throw a NumberFormatException if the parameter is not a valid float.

Example 

String numberAsString = "153.25";
float number = Float.parseFloat(numberAsString);
System.out.println("The number is: " + number);

When you run the code above, the output will show this: 

The number is: 153.25

This is the most common and popular solution when you need to convert a String to float. Note that the resulting value is not an instance of the Float class but just a plain primitive float value. 

 

Convert using Float.valueOf()

The Float.valueOf() static method will return a Float object holding the value of the specified String.

Syntax 

public static Float valueOf(String s) throws NumberFormatException

Note that the method will throw a NumberFormatException if the parameter is not a valid float.

String numberAsString = "153.25";
float number = Float.valueOf(numberAsString);
System.out.println("The number is: " + number);

This will output: 

The number is: 153.25

This is the most common method when you wish to convert a String to Float. Note that the resulting value is an instance of the Float class and not a primitive float value. 

 

 

Convert using new Float(String).floatValue()

Another alternative method is to create an instance of Float class and then invoke it's floatValue() method.

 

Example 

String numberAsString = "153.25";
Float floatObject = new Float(numberAsString);
float number = floatObject.floatValue();

 

We can shorten to: 

String numberAsString = "153.25";
float number = new Float(numberAsString).floatValue();

 

or just: 

float number = new Float("153.25").floatValue();


 

Convert using DecimalFormat

The class java.text.DecimalFormat is a class that can be used to convert a number to it's String representation. It can also be used the other way around - it can parse a String into it's numerical representation. 
Example 

String numberAsString = "153.25";
DecimalFormat decimalFormat = new DecimalFormat("#");
try {
   float number = decimalFormat.parse(numberAsString).floatValue();
   System.out.println("The number is: " + number);
} catch (ParseException e) {
   System.out.println(numberAsString + " is not a valid number.");
}

Will output 

The number is: 153.25

Note that the parse() method will throw a ParseException when there are problems encountered with the String. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值