I want to format decimal value as string with a defined precision in F#. While using printf/sprintf I get:
let sdecimal = sprintf "%.2M" 0.1234M;;
val sdecimal : string = "0.1234"
while for float it works.
let sfloat = sprintf "%.2f" 0.1234;;
val sfloat : string = "0.12"
Seems like I have to first cast it to float which is both verbose and non-kosher.
Is there a simpler way than using C#'s .ToString() formatting?
OK. It works if the formatting is for float:
let sfdecimal = sprintf "%.2f" 0.1234M;;
val sfdecimal : string = "0.12"
The type information is still preserved as pointed out in the comments by @scrwtp.
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