Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format decimal as a string with defined precision using printf/sprintf in F#?

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?

like image 799
TermoTux Avatar asked Apr 09 '16 19:04

TermoTux


1 Answers

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.

like image 69
TermoTux Avatar answered Oct 12 '22 22:10

TermoTux