bigdecimal

How to convert BigDecimal to Double in spring-data-mongodb framework

匿名 (未验证) 提交于 2019-12-03 08:54:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Spring Data MongoDB mapping by default converts BigDecimal to String. However, I want them to be converted as Double in mongodb. This is required for latter to make queries on this field in mongodb (comparison queries/aggregation queries). How can I reigster my own converter (BigDecimalToDouble / DoubleToBigDecimal) to do this? 回答1: Here's how you can add your own converters: <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongoDbFactory"/> <constructor-arg ref="mappingConverter"/>

new BigDecimal(double) vs new BigDecimal(String) [duplicate]

匿名 (未验证) 提交于 2019-12-03 08:52:47
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: BigDecimal from Double incorrect value? 3 answers Convert double to BigDecimal and set BigDecimal Precision 8 answers When BigDecimal is used with an input of double and BigDecimal with an input of String different results seem to appear. BigDecimal a = new BigDecimal(0.333333333); BigDecimal b = new BigDecimal(0.666666666); BigDecimal c = new BigDecimal("0.333333333"); BigDecimal d = new BigDecimal("0.666666666"); BigDecimal x = a.multiply(b); BigDecimal y = c.multiply(d); System.out.println(x);

NumberFormatException: Infinite or NaN

匿名 (未验证) 提交于 2019-12-03 08:51:18
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a method that takes n and returns nth Fibonacci number. Inside the method implementation I use BigDecimal to get the nth Fibonacci number then I use method toBigInteger() to get the number as a BigInteger object and that's surely because I am working with huge numbers in my application. I keep getting correct results until I pass 1475 as an argument for my method. I get NumberFormatException: Infinite or NaN in this case without any clear reason for me. Could you please explain me why am I getting this exception? Here's my method:

Get all decimal places in a number after division

匿名 (未验证) 提交于 2019-12-03 08:44:33
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am currently using the BigDecimal and it is giving me more decimals but not nearly enough for what I am trying to do. I need to be able to get all the way to the 10^6 digit. This is my current code BigDecimal num = new BigDecimal(103993/33102.0); pw.println(num.toString()); and it outputs 3.14159265301190249175533608649857342243194580078125 where the number actually has a lot more decimals: http://www.wolframalpha.com/input/?i=103993%2F33102 回答1: You are loosing the precision when evaluating this: 103993/33102.0 as a double division.

Change DecimalFormat locale

做~自己de王妃 提交于 2019-12-03 08:43:01
问题 I have custom DecimalFormat in Edittext's addTextChangedListener method, everything is working perfectly but when I change language (locale) my addTextChangedListener is not working. double answer = inputDouble * counterToDouble; DecimalFormat df = new DecimalFormat("##.########"); // df=(DecimalFormat)numberFormat; df.setRoundingMode(RoundingMode.DOWN); answer = Double.parseDouble(df.format(answer)); unicoinsAmmount.setText(String.valueOf(df.format(answer))); I searched about my problem and

Java BigDecimal without E

匿名 (未验证) 提交于 2019-12-03 08:35:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a BigDecimal variable BigDecimal x = new BigDecimal("5521.0000000001"); Formula: x = x.add(new BigDecimal("-1") .multiply(x.divideToIntegralValue(new BigDecimal("1.0")))); I want to remove the integer part, to get the value x = ("0.0000000001") , but my new value is 1E-10 and not the 0.0000000001. 回答1: To get a String representation of the BigDecimal without the exponent part, you can use BigDecimal.toPlainString() . In your example: BigDecimal x = new BigDecimal("5521.0000000001"); x = x.add(new BigDecimal("-1"). multiply(x

Java, extract just the fractional part of a BigDecimal?

笑着哭i 提交于 2019-12-03 08:04:14
问题 In Java, I'm working with the BigDecimal class and part of my code requires me to extract the fractional part from it. BigDecimal does not appear to have any built in methods to help me get the number after the decimal point of a BigDecimal . For example: BigDecimal bd = new BigDecimal("23452.4523434"); I want to extract the 4523434 from the number represented above. What's the best way to do it? 回答1: I would try bd.remainder(BigDecimal.ONE) . Uses the remainder method and the ONE constant.

Converting BigDecimal to Integer

匿名 (未验证) 提交于 2019-12-03 07:50:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have Hibernate method which returns me a BigDecimal. I have another API method to which I need to pass that number but it accepts Integer as parameter. I cannot change return types or variable types of both methods. Now how to convert the BigDecimal into Integer and pass it to second method? Is there a way out of this? 回答1: You would call myBigDecimal.intValueExact() (or just intValue() ) and it will even throw an exception if you would lose information. That returns an int but autoboxing takes care of that. 回答2: Can you guarantee that the

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

痞子三分冷 提交于 2019-12-03 07:25:55
策略模式和工厂模式的搭配使用可以很好地消除代码 if-else 的多层嵌套 需求 针对店下商铺,有这样一个需求,对用户客户分为了普通客户、 vip 客户、超级 vip 用户、专属 vip 用户 4 个等级,每当用户购买商品时,针对不同的用户等级和消费金额采取不同的打折优惠策略。在平常的开发当中,必然会出现多层的 if-else 嵌套判断,先判断用户的等级再判断用户购买商品的消费金额。 弊端 以上的情况出现了多层的 if-else 嵌套,除此之外,以后如果需求再有变动,需要再增加一个用户等级,那么又会再次添加 if-else 的嵌套判断,那么如何解决上述的弊端呢,采用策略模式和工厂模式的搭配使用,可以很好地优化多层 if-else 的多层嵌套 实现 编写用户等级枚举类 java">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;

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

元气小坏坏 提交于 2019-12-03 07:03:52
策略模式和工厂模式的搭配使用可以很好地消除代码 if-else 的多层嵌套 需求 针对店下商铺,有这样一个需求,对用户客户分为了普通客户、 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;