I have a problem but I don't know how to solve it: I need to get a Long number from a String (I get this string in a SAXparser class I wrote to get some fields from an XML file).
Here you are the problematic code:
if (bprice) {
String price = new String(ch, start, length);
System.out.println("Product price: " + price);
bprice = false;
if (price.equalsIgnoreCase("0"))
product.price = null;
else product.price = Long.parseLong(price);
}
(product is an instance from an object where I store all the product information. I got the error in the last line)
I debugged it and I got this:
Exception occurred in target VM: For input string: "1.04" java.lang.NumberFormatException: For input string: "1.04" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Long.parseLong(Long.java:419) at java.lang.Long.parseLong(Long.java:468) at parsers.LectorXML.characters(LectorXML.java:325) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.characters(AbstractSAXParser.java:538) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:464) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205) at parsers.LectorXML.leer(LectorXML.java:71) at parsers.LeerXML.main(LeerXML.java:20) <
longor floating point numbers to store prices, unless the prices are expressed in cents (104 instead of 1.04). The most precise type for storing monetary data isBigDecimalDouble?Doublewill accumulate errors when you do math with it because its representation of decimal fractions is not precise.BigDecimalinsteadDoublefor the prices. I experienced it is slower than working withDoubles