I've brought up a similar question in the #beginners stream before, but I'd actually like to ask if there have been any discussions/plans to include something like Num.toStrF (e.g., Num.toStrF "%.1f" 8) in the standard library itself.
As a Roc beginner, I've found myself needing to format numbers a bit more flexibly and I've even rolled out a rudimentary implementation of my own for formatting numbers with a certain number of digits after the decimal point, which has been generally ok.
However, it seems to me that until one (or more) user package crystallise as the de-facto battle-tested standard, this might become a generally common situation whereby users might need to re-invent the wheel (quite likely in a not entirely bug-free way in many cases) over and over, due to the associated difficulties in discoverability of packages until they are solid and well-known enough.
Also, it feels to me that a language level implementation might be more performant, as it may not necessarily be written in Roc itself but be based directly on the language in which the compiler is written in. This point I'm not entirely sure about, so this is only discussed by intuition, without having any proper further insight into this, at the current stage.
Thanks for your time, as always!
I totally think we should have a number formatting package!
I don't think it needs to be a builtin though
Understood, thank you!
in general, I think it's better to make a typed DSL than to use strings for something like this, because that way:
% in %.1f means; it could be a special symbol for the formatter to interpret, but it could also just mean "put a % sign here because this is a percentage")https://package.elm-lang.org/packages/cuducos/elm-format-number/latest/FormatNumber is an example of a typed DSL like that in Elm; something similar could be implemented in Roc! :smiley:
it can be more self-descriptive (e.g. I'm personally not sure offhand what the % in %.1f means; it could be a special symbol for the formatter to interpret, but it could also just mean "put a % sign here because this is a percentage")
Yes, you're right (I also agree regarding the other 2 points). For people not necessarily familiar with C-style language formatting, this wouldn't be interpreted unambiguously. Also, across languages, there may be some differences in behaviour and/or interpretation as well.
Thanks for referencing an Elm package! I have to admit I did come across it in my quick search on how those kinds of things are done in Elm (but I wasn't convinced that that was the de facto way), and now that you've confirmed that you wouldn't be against something like this introduced as a user package, it does make more sense. I'll still need to do a bit of convincing (myself), but now that I have a confirmation regarding Roc's direction in this particular regard, at least I could have a think regarding how I could expand the functionality that I've written for myself, into something that could potentially be useful to other people as well (and I'll make sure to gain sufficient understanding of what kind of trade-offs/decisions they faced/made in the referenced elm-format-number on Elm's side).
Thanks!
happy to help! :smiley:
Hristo has marked this topic as resolved.
Last updated: Jun 16 2026 at 16:19 UTC