博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java BigDecimal round()方法与示例
阅读量:2529 次
发布时间:2019-05-11

本文共 2479 字,大约阅读时间需要 8 分钟。

BigDecimal类round()方法 (BigDecimal Class round() method)

  • round() method is available in java.math package.

    round()方法在java.math包中可用。

  • round() method is used to get a rounded BigDecimal based on the given MathContext setting when the precision value is not equal to 0 otherwise there is no effect of rounding when the precision value is equal to 0.

    当精度值不等于0时,使用round()方法基于给定的MathContext设置获取舍入的BigDecimal,否则,当精度值等于0时就没有舍入效果。

  • round() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    round()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • round() method may throw an exception at the time of rounding.

    round()方法在四舍五入时可能会引发异常。

    ArithmeticException: This exception may throw when this BigDecimal need rounding and the mode of rounding is set to "UNNECESSARY".

    ArithmeticException :当此BigDecimal需要舍入并且舍入模式设置为“ UNNECESSARY”时,可能引发此异常。

Syntax:

句法:

public BigDecimal round(MathContext ma_co);

Parameter(s):

参数:

  • MathContext ma_co – represents the MathContext to use in rounding BigDecimal.

    MathContext ma_co –表示四舍五入BigDecimal时使用的MathContext。

Return value:

返回值:

The return type of this method is BigDecimal, it returns the rounded BigDecimal based on the given MathContext.

此方法的返回类型为BigDecimal ,它根据给定的MathContext返回四舍五入的BigDecimal。

Example:

例:

// Java program to demonstrate the example // of BigDecimal round(MathContext ma_co) method of BigDecimalimport java.math.*;public class RoundOfBD {
public static void main(String args[]) {
// Initialize two variables and // first is of "short" and second is // of "String" type short val1 = 1321; String val2 = "100.24"; // Initialize two BigDecimal objects and // one MathContext BigDecimal b_dec1 = new BigDecimal(val1); BigDecimal b_dec2 = new BigDecimal(val2); MathContext ma_co = new MathContext(4, RoundingMode.CEILING); // round this BigDecimal b_dec1 according to // the given MathContext based on precision // and rounding mode BigDecimal round = b_dec1.round(ma_co); System.out.println("b_dec1.round(ma_co): " + round); // round this BigDecimal b_dec1 according to // the given MathContext based on precision // and rounding mode round = b_dec2.round(ma_co); System.out.println("b_dec2.round(ma_co): " + round); }}

Output

输出量

b_dec1.round(ma_co): 1321b_dec2.round(ma_co): 100.3

翻译自:

转载地址:http://lzozd.baihongyu.com/

你可能感兴趣的文章
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>