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

StackOverflow https://stackoverflow.com/questions/17494419

  •  02-06-2022
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top