开发者

Splitting a double in two, C#

开发者 https://www.devze.com 2022-12-31 08:50 出处:网络
I\'m attempting to use a double to represent a bit of a dual-value type in a database that must sometimes accept two values, and sometimes accept only one (int).

I'm attempting to use a double to represent a bit of a dual-value type in a database that must sometimes accept two values, and sometimes accept only one (int).

So the field is a float in the database, and in my C# code, it is a double (since mapping it via EF makes it a double for some reason... )

So basically what I want to do .. let's say 2.5 is the value. I want to separate that开发者_如何学JAVA out into 2, and 5. Is there any implicit way to go about this?


Like this:

int intPart = (int)value;
double fractionalPart = value - intPart;

If you want fractionalPart to be an int, you can multiply it by 10n, where n is the number of digits you want, and cast to int.
However, beware of precision loss.

However, this is extremely poor design; you should probably make two fields.

SQL Server's float type is an 8-byte floating-point value, equivalent to C#'s double.


You should be able to implicitly type-cast the double to an int to get the first part and subtract that from the number and multiply that by ten to get the second part

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号