Replacement for snprintf
H. S. Teoh
hsteoh at quickfur.ath.cx
Wed Nov 6 20:22:04 UTC 2019
On Wed, Nov 06, 2019 at 07:43:06PM +0100, Jacob Carlborg via Digitalmars-d wrote:
> In my experience, I think it's best to leave the locale support to a
> separate API. The "snprintf" API is never going to be flexible enough.
> No one is using "snprintf" for serious localization.
> It's not just the decimal point that needs to be localized. There are
> various other number related things that need localization.
> Just have a look at the number formatter in Apple's API . It's
> pretty big. Then they have separate formatters for currency, length,
> mass, interval and more.
Yeah, after thinking about this more, I've come to the same conclusion.
Just use %s for anything that depends on complex locale-dependent
configuration, and wrap your data item in a proxy object that does
whatever it takes to make it work.
float myQuantity = ...;
auto output = format("%s", myQuantity.localeFmt(...));
where localeFmt is some function or wrapper struct overloading toString
that does whatever it takes to format the data in a locale-specific way.
More information about the Digitalmars-d