java.text.ChoiceFormat类的format()方法用于获取附加限制字符串格式的字符串生成器,该格式值的特定限制值作为参数传递,该文本作为参数传递给该方法。
用法:
public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition status)
参数:此方法采用以下参数,如下所示
- number:这是选择格式对象的特定限制,必须为其找到格式并附加格式
- toAppendTo:这是要附加格式的新文本
- status:确定是否没有特殊状态要返回
返回值:此方法以StringBuffer对象的形式返回文本和格式的附加值。
异常:如果toAppendto值为null,则此方法引发NullPointerException。
下面是说明format()方法的示例:
示例1:
// Java program to demonstrate
// format() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// creating and initializing ChoiceFormat
ChoiceFormat cf1
= new ChoiceFormat(
"4#wed| 5#thu | 6#fri | 7#sat");
// creating and initializing StringBuffer
StringBuffer str = new StringBuffer("Sun");
// getting the required format with appended text
// ChoiceFormat Object
// using format() method
StringBuffer vlaue = cf1.format(6, str, null);
// display the result
System.out.print("Formated text with appended value: "
+ vlaue.toString());
}
catch (NullPointerException e) {
System.out.println("str is null");
System.out.println("Exception thrown: " + e);
}
}
}
输出:
Formated text with appended value: Sunfri
示例2:
// Java program to demonstrate
// format() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// creating and initializing ChoiceFormat
ChoiceFormat cf1
= new ChoiceFormat(
"4#wed| 5#thu | 6#fri | 7#sat");
// creating and initializing StringBuffer
StringBuffer str = null;
// getting the required format with appended text
// ChoiceFormat Object
// using format() method
StringBuffer vlaue = cf1.format(6, str, null);
// display the result
System.out.print("Formated text with appended value: "
+ vlaue.toString());
}
catch (NullPointerException e) {
System.out.println("str is null");
System.out.println("Exception thrown: " + e);
}
}
}
输出:
str is null Exception thrown: java.lang.NullPointerException
相关用法
- Java ChoiceFormat hashCode()用法及代码示例
- Java ChoiceFormat parse()用法及代码示例
- Java ChoiceFormat equals()用法及代码示例
- Java ChoiceFormat getLimits()用法及代码示例
- Java ChoiceFormat previousDouble()用法及代码示例
- Java ChoiceFormat getFormats()用法及代码示例
- Java ChoiceFormat setChoices()用法及代码示例
- Java ChoiceFormat applyPattern()用法及代码示例
- Java ChoiceFormat nextDouble(double)用法及代码示例
- Java ChoiceFormat nextDouble(double, boolean)用法及代码示例
- Java ChronoLocalDateTime format()用法及代码示例
- Java ChronoZonedDateTime format()用法及代码示例
- Java SimpleDateFormat format()用法及代码示例
- Java Year format()用法及代码示例
- Java ChronoLocalDate format()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 ChoiceFormat format() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。