bigdecimal

How does Double.toString() work if a fraction number cannot be precisely represented in binary?

此生再无相见时 提交于 2019-12-12 11:33:25
问题 I am unable to understand how Double.toString() works in Java/JVM. My understanding is that in general fraction numbers cannot be represented precisely in floating point types such as Double and Float. For example, the binary representation of 206.64 would be 206.6399999999999863575794734060764312744140625. Then how come (206.64).toString() returns "206.64" instead of "206.6399999999999863575794734060764312744140625"? Test code in Kotlin. @Test fun testBigDecimalToString() { val value = 206

Overriding the JSF converter javax.faces.convert.BigDecimalConverter in favor of a custom converter using @FacesConverter(forClass = BigDecimal.class)

江枫思渺然 提交于 2019-12-12 10:12:00
问题 I have the following general BigDecimal converter (deeply reviewing the code is absolutely superfluous). @FacesConverter(value = "bigDecimalConverter") public class BigDecimalConverter implements Converter { @Inject private CurrencyRateBean currencyRateBean; // Maintains a currency selected by a user in his/her session. private static final int scale = 2; // Taken from an enum. @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { if (!StringUtils

Format BigDecimal without scientific notation with full precision

我的未来我决定 提交于 2019-12-12 07:32:14
问题 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 . 回答1: To preserve the precision for a BigDecimal you need to pass the value in as a

Is there a way to make BigDecimal faster than I have here?

拥有回忆 提交于 2019-12-12 04:23:48
问题 I'm working on some financial analysis software that will need to process large (ish) volumes of data. I'd like to use BigDecimal for the accuracy (some pricing info goes to four or five digits to the right of the decimal) but I was concerned about speed. I wrote the following test app and it appears that BigDecimal can be 90 to 100 times slower than Doubles. I knew there would be a delta, but that's more than I was expecting. Here's a typical output after many trials. BigDecimal took 17944

Does anyone know if this is a Java library bug?

雨燕双飞 提交于 2019-12-11 12:38:31
问题 ** This is a corrected post ** I'm doing some very intricate math calculations using BigDecimal and ran into an error in ONE of several thousand tests. Does anyone see something stupid that I did wrong? I don't think so. The output below (corrected from original post) is one of several traces from the calculations. It seems as if Java simply is only printing the non-BigInteger variables incorrectly because the function works - using x,ix, y and iy. The question is: why would the value of x

How to use BigDecimal in swing GUIs?

大憨熊 提交于 2019-12-11 10:54:12
问题 I'm using BigDecimal to represent product prices in a Java SE application. What swing component(s) should I use in order to allow the user to input numbers with only two decimal places and bound it to a BigDecimal variable/Object's property. (Checking that as the user types)? I've been playing with JTextField, JFormattedTextField, NumberFormatter, MaskFormatter but I can't work it out. Is there any combination and configuration of those components to do that? or should I extend the

How can I write SQLite Real vals to Java BigDecimal values?

大兔子大兔子 提交于 2019-12-11 10:04:00
问题 I have a class like this: public class DeliveryItem { private int _id; private String _invoiceNumber; private String _UPC_PLU; private String _vendorItemId; private int _packSize; private String _description; private BigDecimal _cost; private BigDecimal _margin; private BigDecimal _listPrice; private int _departmentNumber; private String _subdepartment; private String _quantity; public void setID(int id) { this._id = id; } public int getID() { return this._id; } . . . The cost, margin, and

Formatting a number with correct decimal scale

余生长醉 提交于 2019-12-11 09:32:46
问题 I need to format a number with scale of 2 decimal places. The original number may be a whole number or a number with three decimal places. However the result should be formatted to have commas and also two decimal places always regardless of whether the original number is whole number or having decimal places. When original num = 56565656.342 ==> I need 56,565,656.34 When original num = 56565656 ==> I need 56,565,656.00 When original num = 56565656.7 ==> I need 56,565,656.70 I am using the

How can I specify the number of decimal places of a BigDecimal to print?

此生再无相见时 提交于 2019-12-11 07:08:38
问题 I am using BigDecimal as follows: BigDecimal val = object.getValue();//it returns BigDecimal type value val contains 12.2300. I want to display 12.23 on my page. How can I do this? 回答1: Use DecimalFormat: String formatted = new DecimalFormat("#.##").format(val); 回答2: you can use DecimalFormat or you can manipulate it with String operations public static String format(BigDecimal big, int precision){ String bigS = big.toString(); int index = bigS.indexOf('.'); String add = bigS.substring(index,

BigDecimal variable padded with random numbers in JDBC Insert

泪湿孤枕 提交于 2019-12-11 07:06:24
问题 Consider SQL statement INSERT INTO table (field) VALUES (-0.11111111) with field Oracle type NUMBER. When the value to be inserted is of type float or double, you get the exact value in field , i.e. -0.11111111. But when the value to be inserted is of type BigDecimal , you get the value padded with random numbers, i.e. 0.1111111099999999990428634077943570446223. Why? Java states that " BigDecimal is an immutable, arbitrary-precision signed decimal numbers." The code is: String sql = "INSERT