Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Dart have sprintf, or does it only have interpolation?

I would like to emulate C's sprintf("%02d", x); in Dart, but I can't find string formatting, only string interpolation.

like image 240
mcandre Avatar asked Feb 15 '12 02:02

mcandre


2 Answers

String interpolation covers most of your needs. If you want to format numbers directly, there is also num.toStringAsPrecision().

like image 72
munificent Avatar answered Oct 19 '22 02:10

munificent


I took a different approach to this issue: by padding the string directly, I don't have to use any libraries (mainly because the intl library seems to be discontinued):

x.toString().padLeft(2, "0");

Would be the equivalent of sprintf("%02d", x);

like image 37
Lukas B. Avatar answered Oct 19 '22 03:10

Lukas B.