UnitsNet icon indicating copy to clipboard operation
UnitsNet copied to clipboard

Is there a reason, the conversion properties return double for quantities with base type decimal?

Open pgrawehr opened this issue 2 years ago • 7 comments

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?

pgrawehr avatar Mar 03 '22 15:03 pgrawehr

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.

tmilnthorp avatar Mar 03 '22 16:03 tmilnthorp

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.

lipchev avatar Mar 03 '22 16:03 lipchev

Although not as convenient, you can also use

BitRate rate = BitRate.FromKilobitsPerSecond(56);
decimal mb = rate.ToUnit(BitRateUnit.MegabitPerSecond).Value;

tmilnthorp avatar Mar 03 '22 19:03 tmilnthorp

Closing this in favor of #875

angularsen avatar Mar 21 '22 06:03 angularsen

Re-opening, realized this describes one of several solutions discussed in #875 .

  • IQuantity.Value should return QuantityValue
  • decimal based quantities should return decimal in their conversion properties, like Information.Bits

angularsen avatar Mar 21 '22 06:03 angularsen

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.

stale[bot] avatar Aug 13 '22 05:08 stale[bot]

Not stale, PR #1074 recently had activity.

angularsen avatar Aug 14 '22 20:08 angularsen

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.

stale[bot] avatar Nov 12 '22 22:11 stale[bot]

Not stale, PR still up for review.

pgrawehr avatar Nov 15 '22 16:11 pgrawehr

Fixed by #1074

angularsen avatar Nov 29 '22 21:11 angularsen