printf("%02d:% 5.2f\n", some_int, some_float)
in Nim (mind the field lengths and the padding)? Where are the docs?
So far, I got this:
echo "$#:$#:$#,$# --> $#:$#:$#,$#".format(h1, m1, s1, ms1, h2, m2, s2, ms2)
Use the unofficial strfmt library, https://lyro.bitbucket.org/strfmt/ referenced from the Nim standard library page.
import strfmt
var
someInt = 3
someFloat = 2.718281828
printfmt("{:02d}:{: .5f}\n", some_int, some_float)
A discussion of this probably belongs on Nim by Example.
Thanks for the answer, Brian. However, I would rather rely on a library that is not flagged "inofficial". I had already stumbled on this one, but had dismissed it for that very reason...
It would be nice to have something basic like this in the standard libs...
zio_tom78: Strfmt looks really nice, but you must have Mercurial in order to install it from the Bitbucket repository.
If you don't have Mercurial installed, you can grab it from the download page directly.
Jehan, thanks for the link, I have immediately downloaded it. The reason why I cited Mercurial is that Nimble needs the hg executable in order to run nimble install strfmt.
Does anybody know if there is a way to tell Nimble to use the .zip package instead of trying to access the remote repository? Running nimble install lyro-strfmt-3f130dcd34a0.zip does not work.
+1 for moving this to the core. Having followed OCaml, I know many arguments for a minimal core, but I find the "batteries included" argument more compelling. Nim is really pretty good, but there are a few omissions and weirdnesses (parseopt2? How about deprecating parseopt?) that would be good to fix.
Araq Perhaps contact the author if he likes to submit it to the core and maintain (!) it there.
Done.
For what it's worth, I'm not a big fan of printf-like constructs. Any formatting language encoded in a string is, at the very least, not extensible (and is also pretty heavyweight; using strfmt adds about 250ms compile time for me, even when everything is cached), and may not play well with other formatting/output modules. I personally use a simple align module that does not have all the bells and whistles of strfmt (though it could easily have the same features), but is lightweight and easily extensible and can be used in conjunction with strutils or subexes:
import strutils
proc `|`*(x: int, d: int): string =
result = $x
let pad = repeatChar(d.abs-len(result))
if d >= 0:
result = pad & result
else:
result = result & pad
proc `|`*(s: string, d: int): string =
let pad = repeatChar(d.abs-len(s))
if d >= 0:
result = pad & s
else:
result = s & pad
proc `|`*(f: float, d: tuple[w,p: int]): string =
result = formatFloat(f, ffDecimal, d.p)
let pad = repeatChar(d.w.abs-len(result), ' ')
if d.w >= 0:
result = pad & result
else:
result = result & pad
proc `|`*(f: float, d: int): string =
$f | d
when isMainModule:
echo "|$1|$2|$3|".format("foo" | -10, 7.1 | (6, 2), 3 | 5)
and is also pretty heavyweight; using strfmt adds about 250ms compile time for me,
That is something we'll look into.