I'm using a fraction to add to the current BigDecimal. It keeps returning an int. Not sure what I'm doing wrong.
//within a class function
public BigDecimal result = new BigDecimal(1);
int x = 2;
double g = 1/x;
result = result.add(new BigDecimal(g));
String s = result.toString();
System.out.println(s);
You have to do the following:
BigDecimal result = new BigDecimal(1);
int x = 2;
double g = 1.0 / x; //<---------- 1.0
result = result.add(new BigDecimal(g));
String s = result.toString();
System.out.println(s);
then there will be a double as outcome
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.