Java Float 類的 toString() 方法返回此 Float 的字符串說明。
與此 Float 對應的原始 float 值轉換為 String,與通過一個參數的 toString() 方法所做的相同。
用法:
public String toString()
參數:
NA
返回值:
toString() 方法返回此 Float 對象的字符串說明。
例子1
public class FloatToStringExample1 {
public static void main(String[] args) {
Float f1=9876f;
System.out.println("1. "+f1+" Converted to String = "+f1.toString());
Float f2=1001f;
System.out.println("2. "+f2+" Converted to String = "+f2.toString());
// instead of adding it will concatenate the two strings
String f3=f1.toString()+f2.toString();
System.out.println("3. Concatenated String = "+f3);
}
}
輸出:
1. 9876.0 Converted to String = 9876.0 2. 1001.0 Converted to String = 1001.0 3. Concatenated String = 9876.01001.0
例子2
import java.util.Scanner;
public class FloatToStringExample2 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter your password:");
Float f1 =scanner.nextFloat();
//calling static toString() method
String f2 = Float.toString(f1);
System.out.println("Your password + 18 = "+f2+18);
}
}
輸出:
Enter your password:923 Your password + 18 = 923.018
例子3
public class FloatToStringExample3 {
public static void main(String[] args) {
int f1 = Float.MIN_EXPONENT;
int f2 = Float.MAX_EXPONENT;
String f3 = Float.toString(f1)+Float.toString(f2);
System.out.println("1. Sum = "+f3);
System.out.println();
Float f4=Float.NEGATIVE_INFINITY;
System.out.println("String value = "+f4.toString());
Float f5= Float.MAX_VALUE;
System.out.println("String value = "+f5.toString());
String s2 = f4.toString()+f5.toString();
System.out.println("2. Sum = "+s2);
}
}
輸出:
1. Sum = -126.0127.0 String value = -Infinity String value = 3.4028235E38 2. Sum = -Infinity3.4028235E38
相關用法
- Java Float toHexString()用法及代碼示例
- Java Float parseFloat()用法及代碼示例
- Java Float equals()用法及代碼示例
- Java Float hashCode()用法及代碼示例
- Java Float intBitsToFloat()用法及代碼示例
- Java Float valueOf()用法及代碼示例
- Java Float longValue()用法及代碼示例
- Java Float intValue()用法及代碼示例
- Java Float byteValue()用法及代碼示例
- Java Float floatValue()用法及代碼示例
- Java Float doubleValue()用法及代碼示例
- Java Float sum()用法及代碼示例
- Java Float isFinite()用法及代碼示例
- Java Float isNaN()用法及代碼示例
- Java Float floatToIntBits()用法及代碼示例
- Java Float compareTo()用法及代碼示例
- Java Float max()用法及代碼示例
- Java Float shortValue()用法及代碼示例
- Java Float isInfinite()用法及代碼示例
- Java Float floatToRawIntBits()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Float toString() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。