UnitsNet
UnitsNet copied to clipboard
Is there a reason, the conversion properties return double for quantities with base type decimal?
Some quantities, for instance information
have decimal
as base type (which does make sense). However, except for the Value
property, all properties return double:
/// <summary>
/// Gets a <see cref="double"/> value of this quantity converted into <see cref="InformationUnit.Bit"/>
/// </summary>
public double Bits => As(InformationUnit.Bit);
/// <summary>
/// Gets a <see cref="double"/> value of this quantity converted into <see cref="InformationUnit.Byte"/>
/// </summary>
public double Bytes => As(InformationUnit.Byte);
This is a bit confusing and unnecessarily looses precision. Would it be possible to change these properties to decimal
, too?
I agree it's not great.
We just closed #1051 which would have deprecated the double conversion properties in favor of returning a Quantity
type. You would then be able to use the Value
property (or IConvertible
interfaces) to retrieve the result as a decimal
.
Feel free to look over the arguments there, but I do feel that returning a Quantity
gives you more power/flexibility in terms of what you can do with the result.
There is also #875 for you (all) to consider- it's not the most straightforward approach and I haven't thought it through a 100% - but that's why I put it up for discussion.
Although not as convenient, you can also use
BitRate rate = BitRate.FromKilobitsPerSecond(56);
decimal mb = rate.ToUnit(BitRateUnit.MegabitPerSecond).Value;
Closing this in favor of #875
Re-opening, realized this describes one of several solutions discussed in #875 .
-
IQuantity.Value
should returnQuantityValue
-
decimal
based quantities should returndecimal
in their conversion properties, likeInformation.Bits
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Not stale, PR #1074 recently had activity.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Not stale, PR still up for review.
Fixed by #1074