简体   繁体   中英

Possible to convert from integer to decimal?

I've run into a bit of a problem when trying to get a decimal result from a query. Initially, I had two int columns, and my goal was to return a decimal value. However, from what I've been told, a decimal can only be returned if both columns are float and if the method in Java also is a float, otherwise it would either round the result or (if Java returned a float) return the result in a zero after the decimal.

In short, is it true that the only way to get a true decimal value is by using two float columns rather than int?

It is not true. If you have two int columns and eg you return the result of dividing one value by the other, the result can be a decimal value if proper care is taken not to round back into an int.

No, its not at all true you can check like ::

ResultSet rs =  // resultset after a query execution 
if(rs.next())
{

double v1 = (Double)rs.getDouble(1);
int v2  = (Integer)rs.getInt(2);
}

This snippet runs fine..

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM