bigdecimal

What type would you map BigDecimal in Java/Hibernate in MySQL?

落爺英雄遲暮 提交于 2019-12-03 06:30:34
问题 After going through the previous develop's trainwreck of code I realized I need to move all of the money based columns to not use floating point math. On the Java side this means using BigDecimal but when using Hibernate/JPA and MySQL 5 what would be the appropriate MySQL data type to make that column? 回答1: DECIMAL and NUMERIC. The recommended Java mapping for the DECIMAL and NUMERIC types is java.math.BigDecimal. The java.math.BigDecimal type provides math operations to allow BigDecimal

Determine if a String is a number and convert in Java?

纵然是瞬间 提交于 2019-12-03 05:08:47
问题 I know variants of this question have been asked frequently before (see here and here for instance), but this is not an exact duplicate of those. I would like to check if a String is a number, and if so I would like to store it as a double . There are several ways to do this, but all of them seem inappropriate for my purposes. One solution would be to use Double.parseDouble(s) or similarly new BigDecimal(s) . However, those solutions don't work if there are commas present (so "1,234" would

How to use comparison operators like >, =, < on BigDecimal

半世苍凉 提交于 2019-12-03 04:50:34
问题 I have a domain class with unitPrice set as BigDecimal data type. Now I am trying to create a method to compare price but it seems like I can't have comparison operators in BigDecimal data type. Do I have to change data type or is there other way around? 回答1: Every object of the Class BigDecimal has a method compareTo you can use to compare it to another BigDecimal. The result of compareTo is then compared > 0 , == 0 or < 0 depending on what you need. Read the documentation and you will find

Format BigDecimal without scientific notation with full precision

被刻印的时光 ゝ 提交于 2019-12-03 04:21:51
I'd like to convert a BigDecimal to String for printing purposes but print out all digits without scientific notation. For example: BigDecimal d = BigDecimal.valueOf(12334535345456700.12345634534534578901); String out = d.toString(); // Or perform any formatting that needs to be done System.out.println(out); I'd like to get 12334535345456700.12345634534534578901 printed. Right now I get: 1.23345353454567E+16 . To preserve the precision for a BigDecimal you need to pass the value in as a String BigDecimal d = new BigDecimal("12334535345456700.12345634534534578901"); System.out.println(d

adding 2 BigDecimal values [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-12-03 04:20:36
This question already has an answer here: Addition for BigDecimal 10 answers 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 void main(String[] args) { Point p = new Point(1.0, 1.0); p.addFiveToCoordinate("x"); p.show(); } } Ok, I would like to add 2

Excel-Boot(一款Excel导入导出解决方案组成的轻量级开源组件)

杀马特。学长 韩版系。学妹 提交于 2019-12-03 03:57:16
Easy-POI 工具链接 Github:https://github.com/programmeres/easy-poi 码云:https://gitee.com/nw1992/easy-poi Easy-POI是一款Excel导入导出解决方案组成的轻量级开源组件。 (如果喜欢或愿意使用, 请star并且Watch本项目, 如果是企业使用, 请通过修改本文件的企业列表告诉我企业名称, 借此给我们鼓励及动力持续维护本项目。) 发现解决bug并已自测,pullRequest后,可以通过邮件告知我们(magic_app@126.com), 第一时间合并并且发布最新版本 使用企业列表: 功能简介 浏览器导出Excel文件(支持单/多sheet) 浏览器导出Excel模板文件 指定路径生成Excel文件(支持单/多sheet) 返回Excel文件(支持单/多sheet)的OutputStream, 一般用于将Excel文件上传到远程, 例如FTP 导入Excel文件(支持单/多sheet) 解决的问题 1.解决导出大量数据造成的内存溢出问题(支持分页查询数据库、采用poi官方推荐api(SXSSFWorkbook), 实现指定行数刷新到磁盘) 2.解决导入大量数据造成的内存溢出问题(分页插入数据库、采用poi官方推荐api(XSSF and SAX),采用SAX模式一行行读取到内存当中去)

高精度小数BigDecimal+二分——java

浪尽此生 提交于 2019-12-03 02:46:07
高精度小数第一题 import java.util.*; import java.math.*; public class Main { public static void main(String []args) { Scanner cin=new Scanner(System.in); //求最靠近sqrt(5)的值mid BigDecimal esp=new BigDecimal("0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"); BigDecimal L=new BigDecimal("2"); BigDecimal R=new BigDecimal("3"); BigDecimal mid=L; while(true) { BigDecimal dis=R.subtract(L); if(dis.abs().compareTo(esp)<=0)break; mid=L.add(R).divide(new BigDecimal("2")); if(mid.multiply(mid).compareTo(new BigDecimal("5"))<=0)//mid偏小 L

Android - exact mathematical calculation

匿名 (未验证) 提交于 2019-12-03 02:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have got a Problem, I am developing an Application which should be able to do some mathematic calculations. These calculations have to be exact (or rather not obviously wrong) But this simple Code double a = 3.048d; double b = 1000d; double c = a / b; gives me a wrong result c is not 0.003048 as expected instead it is 0.0030480000000000004 which is obviously wrong. double d = 3.048 / 1000; this second code-snipet gives the correct result. I am aware that all floatingpoint arithmetic is not exact when calculating with computers but I don't

大数类

女生的网名这么多〃 提交于 2019-12-03 02:33:55
当所需要的计算的数字特别大的时候,超出了数据类型的范围,如果还想要计算话就需要用到大数类,但是虽然大数类支持大数的计算,但是如果遇到算两个大数的乘方,还是需要很长时间的计算,因此尽可能要少使用。但是如果在用double或者float类型的时候出现了,精度不准的情况,还需要用大数类,代码如下: package Test; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; public class Test { public static double round1(double num,int scale) { return new BigDecimal(num).divide(new BigDecimal(1.0),scale,RoundingMode.HALF_UP).doubleValue(); /* * 向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为向上舍入的舍入模式。 如果舍弃部分 >= 0.5,则舍入行为与 ROUND_UP 相同;否则舍入行为与 ROUND_DOWN 相同。 * */ } public static double round2(double num,int scale) { return new BigDecimal

Why does new BigDecimal(“0.0”).stripTrailingZeros() have a scale of 1?

匿名 (未验证) 提交于 2019-12-03 02:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Running this simple program: public static void main(final String... args) { System.out.println(BigDecimal.ZERO.scale()); System.out.println(new BigDecimal("0").scale()); System.out.println(new BigDecimal("0.0").stripTrailingZeros().scale()); System.out.println(new BigDecimal("1.0").stripTrailingZeros().scale()); } outputs: 0 0 1 0 My question is rather simple: why doesn't the third println output 0 ? That would seem logical... EDIT : OK, so, this is a very old bug: Bug Link and in fact, it "works" for any number of zeroes: new BigDecimal("0