bigdecimal

【讲古堂】浮点数

假如想象 提交于 2019-12-03 20:12:07
【 讲古堂 】 浮点数 ( dubenju@126.com 2015/12/19 ) 众所周知,由于用高低电平的电路很容易实现二进制,所以在计算机中普遍采用二进制来存储数据。对应的二进制的位用 Bit 来表示。 1 字节 =8Bits ,如果考虑符号的话,那么一个字节能存储的数值范围是 -128 到 127 。只能是整数,不能是小数。那么小数怎么办呢? 小数的话,一定要有小数点的。如果把小数点固定在一个不变的位置的话,就成了定点数。比如 Oracle 数据库的 NUMBER(4, 2) 则能存储 17.25 或 0.50 这样的数。 定点数 (Fixed Point Number) 所谓定点数,即约定数据的小数点位置是固定不变的。通常将定点数据表示成纯小数或纯整数。为了将数表示成纯小数,通常把小数点固定在数值部分的最高位之前;而为了把数表示成纯整数,则把小数点固定在数值部分的最后面。 对纯小数进行运算时,要用适当的比例因子进行折算,以免产生溢出,或过多损失精度。 假设用一个 n 位字来表示一个定点数 x= x0 x1 x2 … xn-1 ,其中一位 x0 用来表示数的符号位,其余位数代表它的量值。为了对所有 n 位进行统一处理,符号位 x0 通常放在最左位置,并用数值 0 和 1 分别代表正号和负号。对于任意定点数 x= x0 x1 x2 … xn-1 ,如果 x 表示的是纯小数

BigDecimal使用

扶醉桌前 提交于 2019-12-03 17:04:08
  java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数。在实际应用中,需要对更大或者更小的数进行运算和处理。float和double只能用来做科学计算或者是工程计算,在商业计算中要用java.math.BigDecimal。BigDecimal所创建的是对象,我们不能使用传统的+、-、*、/等算术运算符直接对其对象进行数学运算,而必须调用其相对应的方法。方法中的参数也必须是BigDecimal的对象。构造器是类的特殊方法,专门用来创建对象,特别是带有参数的对象。 初始化BigDecimal对象 加减乘除 add(BigDecimal) BigDecimal对象中的值相加,然后返回这个对象。 subtract(BigDecimal) BigDecimal对象中的值相减,然后返回这个对象。 multiply(BigDecimal) BigDecimal对象中的值相乘,然后返回这个对象。 divide(BigDecimal) BigDecimal对象中的值相除,然后返回这个对象。 BigDecimal比较 compareTo(BigDecimal) 示例:a.compareTo(b) a>b 返回1a=b 返回0a<b 返回-1 来源: https://www.cnblogs

BigDecimal 大数的精确计算

孤人 提交于 2019-12-03 16:43:42
BigDecimal的运算——加减乘除 首先是bigdecimal的初始化 这里对比了两种形式,第一种直接value写数字的值,第二种用string来表示 1 BigDecimal num1 = new BigDecimal(0.005); 2 BigDecimal num2 = new BigDecimal(1000000); 3 BigDecimal num3 = new BigDecimal(-1000000); 4 //尽量用字符串的形式初始化 5 BigDecimal num12 = new BigDecimal("0.005"); 6 BigDecimal num22 = new BigDecimal("1000000"); 7 BigDecimal num32 = new BigDecimal("-1000000"); Bigdecimal类的方法 加法 add()函数 减法subtract()函数 乘法multiply()函数 除法divide()函数 绝对值abs()函数 承接上面初始化Bigdecimal分别用string和数进行运算对比 1 //加法 2 BigDecimal result1 = num1.add(num2); 3 BigDecimal result12 = num12.add(num22); 4 5 //减法 6 BigDecimal

Convert scala.math.BigDecimal to java.math.BigDecimal?

南笙酒味 提交于 2019-12-03 16:22:26
问题 How do I convert a scala.math.BigDecimal to java.math.BigDecimal ? 回答1: No need to double-convert to and from string. val sb = scala.math.BigDecimal(12345) val jb = sb.bigDecimal scala.math.BigDecimal is just a very simple wrapper around java.math.BigDecimal , and it provides wrapped value as one of fields. 来源: https://stackoverflow.com/questions/22475082/convert-scala-math-bigdecimal-to-java-math-bigdecimal

adding 2 BigDecimal values [duplicate]

社会主义新天地 提交于 2019-12-03 14:35:47
问题 This question already has answers here : Addition for BigDecimal (11 answers) Closed 5 years ago . class Point { BigDecimal x; BigDecimal y; Point(double px, double py) { x = new BigDecimal(px); y = new BigDecimal(py); } void addFiveToCoordinate(String what) { if (what.equals("x")) { BigDecimal z = new BigDecimal(5); x.add(z); } } void show() { System.out.print("\nx: " + getX() + "\ny: " + getY()); } public BigDecimal getX() { return x; } public BigDecimal getY() { return y; } public static

策略模式和工厂模式搭配使用

冷暖自知 提交于 2019-12-03 14:30:16
需求 针对店下商铺,有这样一个需求,对用户客户分为了普通客户、 vip 客户、超级 vip 用户、专属 vip 用户 4 个等级,每当用户购买商品时,针对不同的用户等级和消费金额采取不同的打折优惠策略。在平常的开发当中,必然会出现多层的 if-else 嵌套判断,先判断用户的等级再判断用户购买商品的消费金额。 弊端 以上的情况出现了多层的 if-else 嵌套,除此之外,以后如果需求再有变动,需要再增加一个用户等级,那么又会再次添加 if-else 的嵌套判断,那么如何解决上述的弊端呢,采用策略模式和工厂模式的搭配使用,可以很好地优化多层 if-else 的多层嵌套 实现 编写用户等级枚举类 package com.zbiti.ifelse.UserType; /** * 用户类型枚举类 */ public enum UserPayServiceEnum { VIP(1,"Vip"), SUPERVIP(2,"SuperVip"), PARTICULALYVIP(3,"ParticularlyVip"), NORMAL(4,"NormalPayService"); /** * 状态值 */ private int code; /** * 类型描述 */ private String value; private UserPayServiceEnum(int code, String

Is there any IEEE 754 standard implementations for Java floating point primitives?

核能气质少年 提交于 2019-12-03 12:08:41
I'm interested if Java is using IEEE 754 standard for implementing its floating point arithmetic. Here I saw this kind of thing in documentation: operation defined in IEEE 754-2008 As I understand positive side of IEEE 754 is to increase precision of floating point arithmetics so if I'll use double or float in Java would presision of computations be same as in BigDecimal ? And if not than what's the point of using IEEE 754 standard in Math class? I'm interested if Java is using IEEE 754 standard for implementing it's floating point arithmetic. IEEE-754 defines standards for multiple floating

如何将业务代码写得像诗一样(使用注解+单例+工厂去掉一大波if和else判断)

那年仲夏 提交于 2019-12-03 12:05:01
1.订单控制器,提供一个根据商品id和银行渠道id计算商品折后价格的接口: import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.math.BigDecimal; @RestController @RequestMapping("/order") public class OrderController { /** * 根据商品id和银行渠道id计算折扣后的金额 * * @param goodsId 商品id * @param channelId 银行渠道id * @return */ @GetMapping("/calc") @ResponseBody public String calcAmount(Integer goodsId, Integer channelId) { Context context = new

Rounding necessary with BigDecimal numbers

安稳与你 提交于 2019-12-03 10:23:00
I want to set scale of two BigDecimal numbers a and b . as in this example : BigDecimal a = new BigDecimal("2.6E-1095"); BigDecimal b = new BigDecimal("2.7E-1105"); int i = 112, j=1; BigDecimal aa = a.setScale(i+j); BigDecimal bb = b.setScale(i+j); and when i run i have this exception: java.lang.ArithmeticException: Rounding necessary at java.math.BigDecimal.divideAndRound(BigDecimal.java:1439) at java.math.BigDecimal.setScale(BigDecimal.java:2394) at java.math.BigDecimal.setScale(BigDecimal.java:2437) Why rounding is necessary ? if i don't want to make around, what is solution please ? Thanks

How to implement floor modulo for every Number type in Kotlin?

匿名 (未验证) 提交于 2019-12-03 09:14:57
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm currently learning Kotlin and trying to create an extension (infix) method that works on all number types ( Byte , Long , Float , etc.). It should work like Python's % operator: 4 % 3 == 1 // only this is the same as Java's % 4 % -3 == -2 -4 % 3 == 2 -4 % -3 == -1 ...or like Java's Math.floorMod , but it should also work with Double or Float : -4.3 % 3.2 == 2.1000000000000005 or with any possible combination of these types 3 % 2.2 == 0.7999999999999998 3L % 2.2f == 0.7999999999999998 The following works as intended, but only for two