I am trying to get 123456789.12 value out of 123456789.1234
If i use strings, it will be worked straightaway using parsing logic. I don't want to return String. requirement is to return Long.
Here i would like return the output the Long type rather than string..can someone help me on this?
Long is an integer type, and thus can not hold a decimal. Trying to use Long.parseLong would result in a NumberFormatException
. You need to use a representation that deals with the scale as well.
try this for long output:
String str = "123456789.1234";
BigInteger num = new BigInteger(str);
return num.longValue();
try this for float output:
String str = "123456789.1234";
BigDecimal num = new BigDecimal(str).setScale(2, BigDecimal.ROUND_HALF_UP);
return num.floatValue();
The number which you want to return it is not Long.You can return double or float.Also you can use
DecimalFormat df = new DecimalFormat("#.##");
String stringDouble = df.format(yourDouble);;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.