Table of Contents
ToggleConvert String to Float in Java with Examples
將 String 轉換為 float 或 Float 是 Java 中常見的操作,可以使用 DecimalFormat 、 Apache Commons Lang 等方法進行操作, Convert a String to an Float in Java 任務是將該字串轉換為單精度浮點數,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- conversion
| +- string
| +- StringToFloatWithExamplesTest.java
單元測試
Convert a String to Float in Java 提供字串轉換成單精度浮點數等操作。
decimalFormat
Convert a String to Float in Java 使用 DecimalFormat 轉換成單精度浮點數。
@Test
public void decimalFormat() {
try {
String value = "151.88";
Number number = DecimalFormat.getNumberInstance().parse(value);
float result = number.floatValue();
System.out.println(result);
assertEquals(151.88f, result, 0);
value = "-40.88";
number = DecimalFormat.getNumberInstance().parse(value);
result = number.floatValue();
System.out.println(result);
assertEquals(-40.88f, result, 0);
value = "-$1,240.88";
DecimalFormat format = new DecimalFormat("$#,##0.00");
format.setParseBigDecimal(true);
result = format.parse(value).floatValue();
System.out.println(result);
assertEquals(-1240.88f, result, 0);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
151.88
-40.88
-1240.88
decimalFormatThrowException
Convert a String to Float in Java 使用 DecimalFormat 轉換成單精度浮點數,輸入不合格式的字串,會拋出例外。
@Test
public void decimalFormatThrowException() {
try {
String value = "151.88";
Number number = DecimalFormat.getNumberInstance().parse(value);
float result = number.floatValue();
System.out.println(result);
assertEquals(151.88f, result, 0);
value = "bash-151.88";
number = DecimalFormat.getNumberInstance().parse(value);
result = number.floatValue();
System.out.println(result);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
151.88
java.text.ParseException: Unparseable number: "bash-151.88"
at java.text.NumberFormat.parse(NumberFormat.java:385)
at org.ruoxue.java_147.string.StringToFloatWithExamplesTest.decimalFormatThrowException(StringToFloatWithExamplesTest.java:50)
constructor
Convert a String to Float in Java 使用 Float 建構子,傳入參數字串,建立一個新的 Float 物件。
@Test
public void constructor() {
String value = "151.88";
float result = new Float(value);
System.out.println(result);
assertEquals(151.88f, result, 0);
value = "-40.88";
result = new Float(value);
System.out.println(result);
assertEquals(-40.88f, result, 0);
}
151.88
-40.88
constructorThrowException
使用 Float 建構子,傳入參數字串,建立一個新的 Float 物件,輸入不合格式的字串,會拋出例外。
@Test(expected = NumberFormatException.class)
public void constructorThrowException() {
String value = "151.88";
float result = new Float(value);
System.out.println(result);
assertEquals(151.88f, result, 0);
value = "junit-151.88";
result = new Float(value);
System.out.println(result);
}
151.88
java.lang.NumberFormatException: For input string: "junit-151.88"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
at java.lang.Float.parseFloat(Float.java:451)
at java.lang.Float.<init>(Float.java:532)
at org.ruoxue.java_147.string.StringToFloatWithExamplesTest.constructorThrowException(StringToFloatWithExamplesTest.java:79)
NumberUtils_toFloat
使用 Apache Commons Lang – NumberUtils 轉換成單精度浮點數。
@Test
public void NumberUtils_toFloat() {
String value = "151.88";
float result = NumberUtils.toFloat(value);
System.out.println(result);
assertEquals(151.88f, result, 0);
value = "-40.88";
result = NumberUtils.toFloat(value);
System.out.println(result);
assertEquals(-40.88f, result, 0);
value = "40.88";
result = NumberUtils.toFloat(value, 0);
System.out.println(result);
assertEquals(40.88f, result, 0);
value = "junit-151.88";
result = NumberUtils.toFloat(value, 0);
System.out.println(result);
assertEquals(0f, result, 0);
}
151.88
-40.88
40.88
0.0
StringToFloatWithExamplesTest.java
Java String to Float Examples 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.conversion.string;
import static org.junit.Assert.*;
import java.text.DecimalFormat;
import java.text.ParseException;
import org.apache.commons.lang3.math.NumberUtils;
import org.junit.Test;
public class StringToFloatWithExamplesTest {
@Test
public void decimalFormat() {
try {
String value = "151.88";
Number number = DecimalFormat.getNumberInstance().parse(value);
float result = number.floatValue();
System.out.println(result);
assertEquals(151.88f, result, 0);
value = "-40.88";
number = DecimalFormat.getNumberInstance().parse(value);
result = number.floatValue();
System.out.println(result);
assertEquals(-40.88f, result, 0);
value = "-$1,240.88";
DecimalFormat format = new DecimalFormat("$#,##0.00");
format.setParseBigDecimal(true);
result = format.parse(value).floatValue();
System.out.println(result);
assertEquals(-1240.88f, result, 0);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
@Test
public void decimalFormatThrowException() {
try {
String value = "151.88";
Number number = DecimalFormat.getNumberInstance().parse(value);
float result = number.floatValue();
System.out.println(result);
assertEquals(151.88f, result, 0);
value = "bash-151.88";
number = DecimalFormat.getNumberInstance().parse(value);
result = number.floatValue();
System.out.println(result);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
@Test
public void constructor() {
String value = "151.88";
float result = new Float(value);
System.out.println(result);
assertEquals(151.88f, result, 0);
value = "-40.88";
result = new Float(value);
System.out.println(result);
assertEquals(-40.88f, result, 0);
}
@Test(expected = NumberFormatException.class)
public void constructorThrowException() {
String value = "151.88";
float result = new Float(value);
System.out.println(result);
assertEquals(151.88f, result, 0);
value = "junit-151.88";
result = new Float(value);
System.out.println(result);
}
@Test
public void NumberUtils_toFloat() {
String value = "151.88";
float result = NumberUtils.toFloat(value);
System.out.println(result);
assertEquals(151.88f, result, 0);
value = "-40.88";
result = NumberUtils.toFloat(value);
System.out.println(result);
assertEquals(-40.88f, result, 0);
value = "40.88";
result = NumberUtils.toFloat(value, 0);
System.out.println(result);
assertEquals(40.88f, result, 0);
value = "junit-151.88";
result = NumberUtils.toFloat(value, 0);
System.out.println(result);
assertEquals(0f, result, 0);
}
}
心得分享
Convert a String to an Float in Java with Examples 將 String 實例轉換為 float 或 Float 實例的多種方法, 除了使用 Java 所提供的 DecimalFormat 、 constructor 之外,也有其他選擇使用外部程式庫來轉換成 float ,像是 Apache Commons Lang 等, Java String to Float Examples 提供了幾種常見方法的操作範例。