Table of Contents
ToggleConvert String to Long in Java with Examples
對包含 long 的字串進行數學運算時,通常會使用轉換成 long ,從文本字段或文本區域讀取資料時,輸入的資料都會作為字串接收,如果輸入的資料很長,需要將字串轉換為 long ,可以使用 decode 、 Apache Commons Lang 等方法進行操作, Convert a String to a Long in Java 任務是將該字串轉換為長整數,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- conversion
| +- string
| +- StringToLongTest.java
單元測試
Convert a String to Long in Java 提供字串轉換成長整數等操作。
decode
Convert a String to Long in Java 使用 Long.decode 轉換成長整數。
@Test
public void decode() {
String value = "168";
long result = Long.decode(value);
System.out.println(result);
assertEquals(168, result);
value = "-30";
result = Long.decode(value);
System.out.println(result);
assertEquals(-30, result);
}
168
-30
decodeThrowException
Convert a String to Long in Java 使用 Long.decode 轉換成長整數,輸入不合格式的字串,會拋出例外。
@Test(expected = NumberFormatException.class)
public void decodeThrowException() {
String value = "168";
long result = Long.decode(value);
System.out.println(result);
assertEquals(168, result);
value = "java-168";
result = Long.decode(value);
System.out.println(result);
}
168
java.lang.NumberFormatException: For input string: "springboot-168"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.valueOf(Long.java:776)
at java.lang.Long.decode(Long.java:928)
at org.ruoxue.java_147.string.StringToLongWithExamplesTest.decodeThrowException(StringToLongWithExamplesTest.java:31)
constructor
Convert a String to Long in Java 使用 Long 建構子,傳入參數字串,建立一個新的 Long 物件。
@Test
public void constructor() {
String value = "168";
long result = new Long(value);
System.out.println(result);
assertEquals(168, result);
value = "-30";
result = new Long(value);
System.out.println(result);
assertEquals(-30, result);
}
168
-30
constructorThrowException
使用 Long 建構子,傳入參數字串,建立一個新的 Long 物件,輸入不合格式的字串,會拋出例外。
@Test(expected = NumberFormatException.class)
public void constructorThrowException() {
String value = "168";
long result = new Long(value);
System.out.println(result);
assertEquals(168, result);
value = "springboot-168";
result = new Long(value);
System.out.println(result);
}
168
java.lang.NumberFormatException: For input string: "springboot-168"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.<init>(Long.java:965)
at org.ruoxue.java_147.string.StringToLongWithExamplesTest.constructorThrowException(StringToLongWithExamplesTest.java:56)
NumberUtils_toLong
使用 Apache Commons Lang – NumberUtils 轉換成長整數。
@Test
public void NumberUtils_toLong() {
String value = "168";
long result = NumberUtils.toLong(value);
System.out.println(result);
assertEquals(168, result);
value = "-30";
result = NumberUtils.toLong(value);
System.out.println(result);
assertEquals(-30, result);
value = "30";
result = NumberUtils.toLong(value, 0);
System.out.println(result);
assertEquals(30, result);
value = "springboot-168";
result = NumberUtils.toLong(value, 0);
System.out.println(result);
assertEquals(0, result);
}
168
-30
30
0
StringToLongWithExamplesTest.java
Java String to Long Examples 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.conversion.string;
import static org.junit.Assert.*;
import org.apache.commons.lang3.math.NumberUtils;
import org.junit.Test;
public class StringToLongWithExamplesTest {
@Test
public void decode() {
String value = "168";
long result = Long.decode(value);
System.out.println(result);
assertEquals(168, result);
value = "-30";
result = Long.decode(value);
System.out.println(result);
assertEquals(-30, result);
}
@Test(expected = NumberFormatException.class)
public void decodeThrowException() {
String value = "168";
long result = Long.decode(value);
System.out.println(result);
assertEquals(168, result);
value = "springboot-168";
result = Long.decode(value);
System.out.println(result);
}
@Test
public void constructor() {
String value = "168";
long result = new Long(value);
System.out.println(result);
assertEquals(168, result);
value = "-30";
result = new Long(value);
System.out.println(result);
assertEquals(-30, result);
}
@Test(expected = NumberFormatException.class)
public void constructorThrowException() {
String value = "168";
long result = new Long(value);
System.out.println(result);
assertEquals(168, result);
value = "springboot-168";
result = new Long(value);
System.out.println(result);
}
@Test
public void NumberUtils_toLong() {
String value = "168";
long result = NumberUtils.toLong(value);
System.out.println(result);
assertEquals(168, result);
value = "-30";
result = NumberUtils.toLong(value);
System.out.println(result);
assertEquals(-30, result);
value = "30";
result = NumberUtils.toLong(value, 0);
System.out.println(result);
assertEquals(30, result);
value = "springboot-168";
result = NumberUtils.toLong(value, 0);
System.out.println(result);
assertEquals(0, result);
}
}
心得分享
Convert a String to a Long in Java with Examples 將 String 實例轉換為 long 或 Long 實例的多種方法, 除了使用 Java 所提供的 decode 、 constructor 之外,也有其他選擇使用外部程式庫來轉換成 long ,像是 Apache Commons Lang 等, Java String to Long Examples 提供了幾種常見方法的操作範例。