java 四写五入涉及的类。

2024-01-18 21:08
文章标签 java 涉及 五入 四写

本文主要是介绍java 四写五入涉及的类。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

 

 1:问题如何把double dou=1234.5678;进行保留两位的四写五入为dou=1234.57

   BigDecimal bigDecimal = new BigDecimal(dou,new MathContext(6,RoundingMode.HALF_EVEN));
  System.out.println("round ---->"+bigDecimal.doubleValue());

这6指定要显示的长度。

 

2.Math.ceil求最小的整数但不小于本身.  
  Math.round求本身的四舍五入。  
  Math.floor求最大的整数但不大于本身.  

 

 

 

 

问题

我要进行四舍五入或取近似值.

解决办法

用 Math.round( ) 进行四舍五入, Math.floor( ) 和 Math.ceil( ) 进行上下近似值。NumberUtilities.round( ) 方法可自定义取值。

讨论

很多情况我们需要得到整数部分而不是带有小数的浮点数。比如计算出结果为 3.9999999 ,期望的结果应该是4.0。

Math.round( ) 方法进行四舍五入计算:

trace(Math.round(204.499)); // 显示: 204

trace(Math.round(401.5)); // 显示: 402

Math.floor( ) 方法去掉小数部分,Math.ceil( ) 方法去掉小数部分后自动加1:

trace(Math.floor(204.99)); // 显示: 204

trace(Math.ceil(401.01)); // 显示: 402

如果我想要把90.337 四舍五入到 90.34,可以这么写:

trace (Math.round(90.337 / .01) * .01); //显示: 9.34

trace (Math.round(92.5 / 5) * 5); // 显示: 95

trace (Math.round(92.5 / 10) * 10); // 显示: 90

更好的办法是用自定义函数NumberUtilities.round( ) ,它需要两个参数:

number :要舍入的数字

roundToInterval :间隔值

NumberUtilities 类在 ascb.util 包中。

imported ascb.util.NumberUtilities导入

trace(NumberUtilities.round(Math.PI)); // Displays: 3

trace(NumberUtilities.round(Math.PI, .01)); // Displays: 3.14

trace(NumberUtilities.round(Math.PI, .0001)); // Displays: 3.1416

trace(NumberUtilities.round(123.456, 1)); // Displays: 123

trace(NumberUtilities.round(123.456, 6)); // Displays: 126

trace(NumberUtilities.round(123.456, .01)); // Displays: 123.46

 

来源于http://esffor.javaeye.com/blog/96075

 

Math.round(par)实现机制内部调用(long)Math.floor(par+1/2)

 

public static long round(double a)
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:

 

(long)Math.floor(a + 0.5d)

Special cases:

  • If the argument is NaN, the result is 0.
  • If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE.
  • If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.

 

 

 

 

这篇关于java 四写五入涉及的类。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/620256

相关文章

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

Spring Boot 整合 SSE(Server-Sent Events)实战案例(全网最全)

《SpringBoot整合SSE(Server-SentEvents)实战案例(全网最全)》本文通过实战案例讲解SpringBoot整合SSE技术,涵盖实现原理、代码配置、异常处理及前端交互,... 目录Spring Boot 整合 SSE(Server-Sent Events)1、简述SSE与其他技术的对

Spring Security 前后端分离场景下的会话并发管理

《SpringSecurity前后端分离场景下的会话并发管理》本文介绍了在前后端分离架构下实现SpringSecurity会话并发管理的问题,传统Web开发中只需简单配置sessionManage... 目录背景分析传统 web 开发中的 sessionManagement 入口ConcurrentSess

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro

Java实现本地缓存的四种方法实现与对比

《Java实现本地缓存的四种方法实现与对比》本地缓存的优点就是速度非常快,没有网络消耗,本地缓存比如caffine,guavacache这些都是比较常用的,下面我们来看看这四种缓存的具体实现吧... 目录1、HashMap2、Guava Cache3、Caffeine4、Encache本地缓存比如 caff

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制

Java高效实现Word转PDF的完整指南

《Java高效实现Word转PDF的完整指南》这篇文章主要为大家详细介绍了如何用Spire.DocforJava库实现Word到PDF文档的快速转换,并解析其转换选项的灵活配置技巧,希望对大家有所帮助... 目录方法一:三步实现核心功能方法二:高级选项配置性能优化建议方法补充ASPose 实现方案Libre

springboot整合mqtt的步骤示例详解

《springboot整合mqtt的步骤示例详解》MQTT(MessageQueuingTelemetryTransport)是一种轻量级的消息传输协议,适用于物联网设备之间的通信,本文介绍Sprin... 目录1、引入依赖包2、yml配置3、创建配置4、自定义注解6、使用示例使用场景:mqtt可用于消息发

Java List 使用举例(从入门到精通)

《JavaList使用举例(从入门到精通)》本文系统讲解JavaList,涵盖基础概念、核心特性、常用实现(如ArrayList、LinkedList)及性能对比,介绍创建、操作、遍历方法,结合实... 目录一、List 基础概念1.1 什么是 List?1.2 List 的核心特性1.3 List 家族成