setParseIntegerOnly()方法是java.text.NumberFormat的内置方法,该方法设置此NumberFormat中使用的RoundingMode。处理不同舍入模式的子类应重写此方法。
用法:
public void setRoundingMode(RoundingMode mode)
参数:该函数接受强制参数模式,该模式指定要设置的模式
返回值:该函数不返回任何内容,因此返回类型为void。
异常:该函数引发两个异常,如下所述:
- UnsupportedOperationException:如果默认实现始终抛出此异常
- NullPointerException :如果roundingMode为null
下面是上述函数的实现:
程序1:
// Java program to implement
// the above function
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
import java.math.RoundingMode;
public class Main {
public static void main(String[] args)
throws Exception
{
NumberFormat nF
= NumberFormat.getNumberInstance();
System.out.println("set initially as: "
+ nF.getRoundingMode());
// Set grouping
nF.setRoundingMode(RoundingMode.FLOOR);
// Print the final
System.out.println("set finally as: "
+ nF.getRoundingMode());
}
}
输出:
set initially as: HALF_EVEN set finally as: FLOOR
示例2:
// Java program to implement
// the above function
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
import java.math.RoundingMode;
public class Main {
public static void main(String[] args)
throws Exception
{
NumberFormat nF
= NumberFormat.getNumberInstance();
System.out.println("set initially as: "
+ nF.getRoundingMode());
// Set grouping
nF.setRoundingMode(RoundingMode.DOWN);
// Print the final
System.out.println("set finally as: "
+ nF.getRoundingMode());
}
}
输出:
set initially as: HALF_EVEN set finally as: DOWN
示例3:
// Java program to implement
// the above function
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
import java.math.RoundingMode;
public class Main {
public static void main(String[] args)
throws Exception
{
try {
NumberFormat nF
= NumberFormat.getNumberInstance();
System.out.println("set initially as: "
+ nF.getRoundingMode());
// Set grouping
nF.setRoundingMode(null);
// Print the final
System.out.println("set finally as: "
+ nF.getRoundingMode());
}
catch (Exception e) {
System.out.println("Exception is: "
+ e);
}
}
}
输出:
set initially as: HALF_EVEN Exception is: java.lang.NullPointerException
参考: https://docs.oracle.com/javase/10/docs/api/java/math/RoundingMode.html
相关用法
- Java NumberFormat setMaximumIntegerDigits()用法及代码示例
- Java NumberFormat getPercentInstance()用法及代码示例
- Java NumberFormat getAvailableLocales()用法及代码示例
- Java NumberFormat equals()用法及代码示例
- Java NumberFormat getMinimumFractionDigits()用法及代码示例
- Java NumberFormat hashCode()用法及代码示例
- Java NumberFormat getMaximumIntegerDigits()用法及代码示例
- Java NumberFormat getCurrency()用法及代码示例
- Java NumberFormat setParseIntegerOnly()用法及代码示例
- Java NumberFormat parse()用法及代码示例
- Java NumberFormat isParseIntegerOnly()用法及代码示例
- Java NumberFormat getIntegerInstance()用法及代码示例
- Java NumberFormat getNumberInstance()用法及代码示例
- Java NumberFormat getCurrencyInstance()用法及代码示例
- Java NumberFormat getMaximumFractionDigits()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 NumberFormat setRoundingMode() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。