I've a decimal value 34.3287332 how can i get the fraction of the value like .3287332 please any one help (I can convert that to string and get the fraction. but i dont need to)
问题:
回答1:
I would just fetch the whole value, and then get the remainder:
decimal total = /* get value from database */;
decimal fraction = decimal.Remainder(total, 1m);
(There may be a more efficient way to get the remainder part, but this is pretty simple.)
While you could do this in SQL as well, that's likely to work less well when you want to fetch the values with LINQ to SQL or anything similar - I prefer to put value manipulation in the .NET code rather than in the SQL, unless it affects the results or performance.
回答2:
Use FLOOR():
SELECT Value-FLOOR(Value)
This will do:
34.3287332 - 34 = 0.3287332
回答3:
Note that the FLOOR suggestion above will not work correctly for negative numbers; you need to do ABS(my_float_value) - FLOOR(ABS(my_float_value))
to get the right answer.
Should you find yourself using an SQLite backend, you'd find it has no FLOOR function, and the "my_float_value % 1" trick isn't valid either. Then you could apply a CAST to get the equivalent functionality:
ABS(my_float_value) - CAST(ABS(my_float_value) AS INTEGER)