bigdecimal

Override BigDecimal to_s default in Ruby

末鹿安然 提交于 2020-01-02 09:43:27
问题 As I retrieve data from a database table an array is populated. Some of the fields are defined as decimal & money fields and within the array they are represented as BigDecimal. I use these array values to populate a CSV file, but the problem is that all BigDecimal values are by default represented in Scientific format (which is the default behaviour of the BigDecimal to_s method). I can show the values by using to_s('F'), but how can I override the default? 回答1: This is built on @Farrel's

Converting big decimal to double without exponential format

让人想犯罪 __ 提交于 2020-01-01 18:54:20
问题 I'm doing calculations on High decimal precision BigDecimal objects. I'm using a third party library that requires the parameter in double. When I try to convert this to double i get the values in exponential format instead of decimals . BigDecimal 0.000035000000000 Double 3.5E-5 BigDecimal bd; BigDecimal(0.0000349999999995631583260546904057264328002929687500); bd= bd.setScale(15,BigDecimal.ROUND_CEILING); Log.i("bd","bd "+bd.toPlainString()); Log.i("bd","double"+bd.doubleValue()); I have

What is the best way to convert Dollars (Big Decimal) in Cents (Integer) in java?

筅森魡賤 提交于 2020-01-01 09:12:23
问题 I have to integrate my web application with a payment gateway. I want to input total amount in USD and then convert it into Cents as my payment gateway library accepts amount in Cents (of type Integer ). I found that Big Decimal in java is best way for manipulation of currency. Currently I take input as say USD 50 and convert it to Integer like this: BigDecimal rounded = amount.setScale(2, BigDecimal.ROUND_CEILING); BigDecimal bigDecimalInCents = rounded.multiply(new BigDecimal("100.00"));

Check if BigDecimal is integer value

半世苍凉 提交于 2019-12-31 10:58:09
问题 Can anyone recommend an efficient way of determining whether a BigDecimal is an integer value in the mathematical sense? At present I have the following code: private boolean isIntegerValue(BigDecimal bd) { boolean ret; try { bd.toBigIntegerExact(); ret = true; } catch (ArithmeticException ex) { ret = false; } return ret; } ... but would like to avoid the object creation overhead if necessary. Previously I was using bd.longValueExact() which would avoid creating an object if the BigDecimal

BCD math library for arbitrary big numbers?

眉间皱痕 提交于 2019-12-30 08:28:29
问题 I'm looking for a replacement of the stock Delphi Data.FmtBcd library because I just hit its limits like maximum decimal digits it can represent and program terminates with EBcdOverflowException . For the curious, I'm calculating arithmetic series members and need to handle very large numbers - hundred-thousands positions are not so uncommon. And also get results in a reasonable time. I did rewritten part of the code to Python 3.2 for the testing purposes and calculation speed would be

How can I get more than 100 decimal digits in C#?

依然范特西╮ 提交于 2019-12-30 04:05:07
问题 Is it possible to get more than 100 decimal digits in C#? If yes what is the necessary portion of code? In Java there something call BigDecimal but it still can't reach more than 55 digits. 回答1: Using J# libraries: Download the J# Redistributable to obtain the J# class libraries. This article hints about how to use it to get ZIP-ability in your .NET projects, and explains a bit about BigDecimal, but as Ramhound explained, the article is relatively old. After download, add the vsjlib to your

Format of BigDecimal number

谁说胖子不能爱 提交于 2019-12-29 08:05:37
问题 BigDecimal val = BigDecimal.valueOf(0.20); System.out.println(a); I want to store in val a value 0.20 and not 0.2 . What I can do ? I dont think I can use NumberFormat in this case, when I use NumberFormat I must know what the length of my decimal number is! I can have 0.20 or 0.5000, I don't know the exact length of my decimal number, so I cannot use : DecimalFormat df = new DecimalFormat("#0.00"); or DecimalFormat df = new DecimalFormat("#0.00000"); maybe I have just 2 numbers after point

Ruby on Rails nil can't be coerced into BigDecimal

China☆狼群 提交于 2019-12-29 07:04:26
问题 Why do I get nil can't be coerced into BigDecimal when I try to perform a calculation: here's the code: model/drink.rb class Drink < ActiveRecord::Base belongs_to :menu before_save :total_amount def total_amount self.total_amount = self.price * self.quantity end model/menu.rb class Menu < ActiveRecord::Base has_many :drinks, :dependent => :destroy accepts_nested_attributes_for :drinks, :allow_destroy => true #Validations end * Drink is the (nested)child model and Menu the parent model When I

JDK8 stream 在项目中的应用

那年仲夏 提交于 2019-12-28 12:25:04
JDK8 stream会将复杂的代码实现用一行代码搞定。 1、将集合的某一个属性提取出来组成另一个集合 List<Integer> resultList = list.stream().map(Object::getMaintain_id).collect(Collectors.toList()); 2、将集合按照集合里的某一属性转换成map Map<Integer, List<Object>> map = list.stream().collect(Collectors.groupingBy(Object::getSku_id)); 3、对某一列进行求和 int count = list.stream().mapToInt(Object::getCan_returned_count).sum(); 4、集合根据某个属性筛选 List<Object> resultList = list.stream().filter(Object-> Object.getActivity_id() == 0).collect(Collectors.toList()); 5、集合中的某个属性相加 Double value = list.stream().mapToDouble(Object-> (Object.getPromotion_value()).setScale(2, BigDecimal

Is there a C++ equivalent to Java's BigDecimal?

放肆的年华 提交于 2019-12-27 14:44:47
问题 I'm looking for a C++ class that can do decimal floating point arithmetic. Looking through http://speleotrove.com/decimal/ there are links to all sorts of classes that people have written and not maintained. Digging through the decNumber++ stuff led me to some emails showing that GCC will eventually support this functionality. (Formally known as ISO/IEC TR 24733) I'm looking for something I can use as a drop-in replacement for float or double, something that other people are using in their