Hi Bob,

I'm not quite sure how I'd do it using the sprintf function in F#, but doing it the C# way (still from F#) is very easy.

let szfloat = f.ToString("f" ^ n.ToString());

Say, if f = 1.2345678, and you call f.ToString("f3"), then you get 1.234, and if you call f.ToString("f4") then you get 1.2345, etc.

^ is the string concatenation operator in F#. You can also use the + operator on strings as well.

Hope that helps,
-Matt

By on 12/26/2007 12:26 PM ()

Hi guys,

You can also use "*" as a specifier in a precision of width field, where an extra integer argument is accepted by the generated format function, e.g.

1
2
3
4
 

printfn "%10.4f" 3.14157

is equivalent to:

1
2
3
4
 

printfn "%*.*f" 10 4 3.14157

Likewise

1
2
3
4
 

printfn "%*d" 3 4

is equivalent to:

1
2
3
4
 

printfn "%3d" 4
By on 12/29/2007 9:18 AM ()

Holy cow! That's awesome!

By on 12/29/2007 9:24 AM ()

Try again :-)

1
2
3
4
 

printfn "%10.4f" 3.14157

is equivalent to:

1
2
3
4
 

printfn "%*.*f" 10 4 3.14157

Likewise

1
2
3
4
 

printfn "%*d" 3 4

is equivalent to:

1
2
3
4
5
 

printfn "%3d" 4

By on 12/29/2007 11:53 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper