Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you convert a decimal with a unit of measure to a float with the same unit? [duplicate]

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?

like image 810
John Atwood Avatar asked Jul 05 '13 17:07

John Atwood


People also ask

How do you convert a decimal to a float in Python?

There is two methods: float_number = float ( decimal_number ) float_number = decimal_number * 1.0.

Can you convert a decimal to a double?

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.


1 Answers

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))
like image 197
Mgetz Avatar answered Oct 03 '22 07:10

Mgetz