I cannot figure out how to convert a decimal with a unit of measure to a float with a unit of measure.
I have been experimenting with code similar to:
let toFloat (value: decimal<'T>) =
let value = float (value / LanguagePrimitives.GenericOne<decimal<'T>>)
value * LanguagePrimitives.GenericOne<float<'T>>
This method produces a signature of decimal -> float, which is not what I want. I'm trying to create a function of type decimal<'T> -> float<'T>.
Is it possible to create such a function? If so, what would it look like?
There is two methods: float_number = float ( decimal_number ) float_number = decimal_number * 1.0.
You can also convert a Decimal to a Double value by using the Explicit assignment operator. Because the conversion can entail a loss of precision, you must use a casting operator in C# or a conversion function in Visual Basic.
I took a look at it on http://www.tryfsharp.org and it seems that this should work:
let toFloat (value: decimal<'T>) =
LanguagePrimitives.FloatWithMeasure<'T>(float (decimal value))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With