How to read a double value from a String with a certain precision (say 4) assuming the string contains something like “10.1234” using this api
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you want decimal precision,
doubleis the wrong target type, as it is a binary format that cannot accurately represent most round decimal fractions. Thatdoublevalue will actually be rounded to something like 10.123399999999999999976Instead, use
BigDecimalall the way, or forget about runding while you read and manipulate the data, and round it only when you print the result.Read The Floating-Point Guide for more information.