decimalformat

DecimalFormat and Double.valueOf()

可紊 提交于 2019-11-28 23:07:00
问题 I'm trying to get rid of unnecessary symbols after decimal seperator of my double value. I'm doing it this way: DecimalFormat format = new DecimalFormat("#.#####"); value = Double.valueOf(format.format(41251.50000000012343)); But when I run this code, it throws: java.lang.NumberFormatException: For input string: "41251,5" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224) at java.lang.Double.valueOf(Double.java:447) at ... As I see, Double.valueOf() works great with

Java - Decimal Format.parse to return double value with specified number of decimal places

人盡茶涼 提交于 2019-11-28 09:57:13
I want to be able to convert a string to a Double given a number of decimal places in a format string. So "###,##0.000" should give me a Double to 3 decimal places. Edit - added more info to what happens The user enters the value in the UI - which is input into a String. The rule is this value is limited to 3 decimal places. The underlying code stores the value in the database which is then used in a calculation. Therefore the trailing decimal places will cause the calculations to be out slightly to what would be expected. I have the following code: try { // output current locale we are

DecimalFormat pattern

喜你入骨 提交于 2019-11-27 16:25:24
问题 public static String formatAmountUpToTwoDecimalNumber(String amount) { if(amount==null || "".equals(amount)) { return ""; } Double doubleAmount = Double.valueOf(amount); double myAmount = doubleAmount.doubleValue(); NumberFormat f = new DecimalFormat("###,###,###,###,##0.00"); String s = f.format(myAmount); return s; } "###,###,###,###,##0.00" , What exactly is the purpose of this pattern ? I believe it serves two purposes to group numbers, that is put thousand seperator comma to append two

Summing numbers with comma as decimal separator in XSLT?

此生再无相见时 提交于 2019-11-27 15:52:00
I have an XML file where the number are comma-separated <foo> <bar val="1,23"/> <bar val="4,56"/> <bar val="7,89"/> </foo> I would like to make a sum over /foo/bar/@val values in XSLT, but I am a bit stuck the formatting. Does anyone knows what would be the proper syntax? I am guessing, that the value specified in a "val" attribute is a number that has comma instead of a decimal point. Several solutions are possible : I. XSLT 1.0 This transformation: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common" > <xsl:output method="text"/>

Show padding zeros using DecimalFormat

依然范特西╮ 提交于 2019-11-27 13:28:22
I'm using DecimalFormat to format doubles to 2 decimal places like this: DecimalFormat dec = new DecimalFormat("#.##"); double rawPercent = ( (double)(count.getCount().intValue()) / (double)(total.intValue()) ) * 100.00; double percentage = Double.valueOf(dec.format(rawPercent)); It works, but if i have a number like 20, it gives me this: 20.0 and I want this: 20.00 Any suggestions? The DecimalFormat class is for transforming a decimal numeric value into a String. In your example, you are taking the String that comes from the format( ) method and putting it back into a double variable. If you

How can i parse a String to BigDecimal? [duplicate]

馋奶兔 提交于 2019-11-27 07:56:23
This question already has an answer here: Safe String to BigDecimal conversion 9 answers I have this String: 10,692,467,440,017.120 (it's an amount). I want to parse it to a BigDecimal. The problem is that I have tried both DecimalFormat and NumbeFormat in vain. Any help? René Link Try this // Create a DecimalFormat that fits your requirements DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setGroupingSeparator(','); symbols.setDecimalSeparator('.'); String pattern = "#,##0.0#"; DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols); decimalFormat

Java - Decimal Format.parse to return double value with specified number of decimal places

会有一股神秘感。 提交于 2019-11-27 03:25:14
问题 I want to be able to convert a string to a Double given a number of decimal places in a format string. So "###,##0.000" should give me a Double to 3 decimal places. Edit - added more info to what happens The user enters the value in the UI - which is input into a String. The rule is this value is limited to 3 decimal places. The underlying code stores the value in the database which is then used in a calculation. Therefore the trailing decimal places will cause the calculations to be out

How can I truncate a double to only two decimal places in Java? [duplicate]

删除回忆录丶 提交于 2019-11-26 17:35:26
This question already has an answer here: How to round a number to n decimal places in Java 29 answers For example I have the variable 3.545555555, which I would want to truncate to just 3.54. Bozho If you want that for display purposes, use java.text.DecimalFormat : new DecimalFormat("#.##").format(dblVar); If you need it for calculations, use java.lang.Math : Math.floor(value * 100) / 100; Cedric Dubourg DecimalFormat df = new DecimalFormat(fmt); df.setRoundingMode(RoundingMode.DOWN); s = df.format(d); Check available RoundingMode and DecimalFormat . Mani Bit Old Forum, None of the above

Summing numbers with comma as decimal separator in XSLT?

若如初见. 提交于 2019-11-26 17:19:35
问题 I have an XML file where the number are comma-separated <foo> <bar val="1,23"/> <bar val="4,56"/> <bar val="7,89"/> </foo> I would like to make a sum over /foo/bar/@val values in XSLT, but I am a bit stuck the formatting. Does anyone knows what would be the proper syntax? 回答1: I am guessing, that the value specified in a "val" attribute is a number that has comma instead of a decimal point. Several solutions are possible : I. XSLT 1.0 This transformation: <xsl:stylesheet version="1.0" xmlns

How can i parse a String to BigDecimal? [duplicate]

孤街浪徒 提交于 2019-11-26 13:55:08
问题 This question already has an answer here: Safe String to BigDecimal conversion 9 answers I have this String: 10,692,467,440,017.120 (it's an amount). I want to parse it to a BigDecimal. The problem is that I have tried both DecimalFormat and NumbeFormat in vain. Any help? 回答1: Try this // Create a DecimalFormat that fits your requirements DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setGroupingSeparator(','); symbols.setDecimalSeparator('.'); String pattern = "#,##0.0#";