Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 12, 2006 number formatting | ||||
---|---|---|---|---|
| ||||
Are there any number formatting routines in Phobos? I couldn't see any, but thought I'd ask. I simply want to get a number back as a string with thousand separators. If not, I think Win32's GetNumberFormat will do what I want. Cheers. |
January 13, 2006 Re: number formatting | ||||
---|---|---|---|---|
| ||||
Posted in reply to John C | John C escribió: > Are there any number formatting routines in Phobos? I couldn't see any, but thought I'd ask. I simply want to get a number back as a string with thousand separators. > > If not, I think Win32's GetNumberFormat will do what I want. > > Cheers. > > There's std.string.format(). It works just like writef() except that it returns a char[]. -- Carlos Santander Bernal |
January 13, 2006 Re: number formatting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander | "Carlos Santander" <csantander619@gmail.com> wrote in message news:dq6qik$1hed$1@digitaldaemon.com... > John C escribió: >> Are there any number formatting routines in Phobos? I couldn't see any, but thought I'd ask. I simply want to get a number back as a string with thousand separators. >> >> If not, I think Win32's GetNumberFormat will do what I want. >> >> Cheers. > > There's std.string.format(). It works just like writef() except that it > returns > a char[]. > > -- > Carlos Santander Bernal Well, of course I knew about string.format. But is there a format pattern that will, for example, return 1500000 as "1,500,000"? |
January 13, 2006 Re: number formatting | ||||
---|---|---|---|---|
| ||||
Posted in reply to John C | John C escribió: > "Carlos Santander" <csantander619@gmail.com> wrote in message news:dq6qik$1hed$1@digitaldaemon.com... >> John C escribió: >>> Are there any number formatting routines in Phobos? I couldn't see any, but thought I'd ask. I simply want to get a number back as a string with thousand separators. >>> >>> If not, I think Win32's GetNumberFormat will do what I want. >>> >>> Cheers. >> There's std.string.format(). It works just like writef() except that it returns >> a char[]. >> >> -- >> Carlos Santander Bernal > > Well, of course I knew about string.format. But is there a format pattern that will, for example, return 1500000 as "1,500,000"? > > Oops, sorry. I didn't understand that. Apparently there's not, and it seems that C's printf doesn't support that either (AFAICT). -- Carlos Santander Bernal |
January 13, 2006 Re: number formatting | ||||
---|---|---|---|---|
| ||||
Posted in reply to John C | John C schrieb:
> Are there any number formatting routines in Phobos? I couldn't see any, but thought I'd ask. I simply want to get a number back as a string with thousand separators.
>
> If not, I think Win32's GetNumberFormat will do what I want.
>
> Cheers.
>
>
You can use the c-style sprintf to format numbers. Isn't exactly elegant but seems to work. See date.d (in src\phobos\std) for uses.
HienTau
|
January 13, 2006 Re: number formatting | ||||
---|---|---|---|---|
| ||||
Posted in reply to HienTau | "HienTau" <hientau.mail@gmail.com> wrote in message news:dq87pk$iq6$1@digitaldaemon.com... > John C schrieb: >> Are there any number formatting routines in Phobos? I couldn't see any, but thought I'd ask. I simply want to get a number back as a string with thousand separators. >> >> If not, I think Win32's GetNumberFormat will do what I want. >> >> Cheers. > You can use the c-style sprintf to format numbers. Isn't exactly elegant but seems to work. See date.d (in src\phobos\std) for uses. > > HienTau The "C-style sprintf" you mention *is* the C sprintf, and it doesn't handle thousand separators. I'd already tried it. This reminds me that one of the gaping holes in Phobos is support for locales. Open std.dateparse to see how bad it is - English day and month names are hard coded! There should be a localisation module that might incorporate numeric formatting (the latter would probably depend on the former, eg for currencies). Right, I guess I'm looking at rolling my own stuff. |
January 13, 2006 Re: number formatting | ||||
---|---|---|---|---|
| ||||
Posted in reply to John C | John C schrieb:
> "HienTau" <hientau.mail@gmail.com> wrote in message news:dq87pk$iq6$1@digitaldaemon.com...
>> John C schrieb:
>>> Are there any number formatting routines in Phobos? I couldn't see any, but thought I'd ask. I simply want to get a number back as a string with thousand separators.
>>>
>>> If not, I think Win32's GetNumberFormat will do what I want.
>>>
>>> Cheers.
>> You can use the c-style sprintf to format numbers. Isn't exactly elegant but seems to work. See date.d (in src\phobos\std) for uses.
>>
>> HienTau
>
> The "C-style sprintf" you mention *is* the C sprintf, and it doesn't handle thousand separators. I'd already tried it.
>
> This reminds me that one of the gaping holes in Phobos is support for locales. Open std.dateparse to see how bad it is - English day and month names are hard coded! There should be a localisation module that might incorporate numeric formatting (the latter would probably depend on the former, eg for currencies).
>
> Right, I guess I'm looking at rolling my own stuff.
>
>
"Right, I guess I'm looking at rolling my own stuff."
I guess this is *the* motto for convenience d-programming until at least version 1.0 ;-)
|
January 13, 2006 Re: number formatting | ||||
---|---|---|---|---|
| ||||
Posted in reply to John C | "John C" <johnch_atms@hotmail.com> wrote...
[snip]
> This reminds me that one of the gaping holes in Phobos is support for locales. Open std.dateparse to see how bad it is - English day and month names are hard coded! There should be a localisation module that might incorporate numeric formatting (the latter would probably depend on the former, eg for currencies).
>
> Right, I guess I'm looking at rolling my own stuff.
The ICU project has truly excellent support for this kind of thing, although it is industrial strength ~ can be a bit heavyweight for personal use? They have all kinds of tools for externalizing and migrating/managing I18N concerns, and there's a D wrapper for ICU in the Mango library.
I'm also interested in a lightweight implementation (Mango has a bunch of support functions to make it happen) ~ just haven't got around to hooking up the pieces yet. If ICU is not appropriate for your needs, and you think it's a reasonably idea, we might combine efforts?
|
January 13, 2006 Re: number formatting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | "Kris" <fu@bar.com> wrote in message news:dq8o9c$12rs$1@digitaldaemon.com... > "John C" <johnch_atms@hotmail.com> wrote... > [snip] >> This reminds me that one of the gaping holes in Phobos is support for locales. Open std.dateparse to see how bad it is - English day and month names are hard coded! There should be a localisation module that might incorporate numeric formatting (the latter would probably depend on the former, eg for currencies). >> >> Right, I guess I'm looking at rolling my own stuff. > > > The ICU project has truly excellent support for this kind of thing, although it is industrial strength ~ can be a bit heavyweight for personal use? They have all kinds of tools for externalizing and migrating/managing I18N concerns, and there's a D wrapper for ICU in the Mango library. > > I'm also interested in a lightweight implementation (Mango has a bunch of support functions to make it happen) ~ just haven't got around to hooking up the pieces yet. If ICU is not appropriate for your needs, and you think it's a reasonably idea, we might combine efforts? Certainly. I'd be more than happy to collaborate on a proper D library for this. I'm going to study some of the existing packages (.NET, Java, ICU, STL) to see what kind of work is required and how it's implemented. |
January 16, 2006 Re: number formatting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | "Kris" <fu@bar.com> wrote in message news:dq8o9c$12rs$1@digitaldaemon.com... > "John C" <johnch_atms@hotmail.com> wrote... > [snip] >> This reminds me that one of the gaping holes in Phobos is support for locales. Open std.dateparse to see how bad it is - English day and month names are hard coded! There should be a localisation module that might incorporate numeric formatting (the latter would probably depend on the former, eg for currencies). >> >> Right, I guess I'm looking at rolling my own stuff. > > > The ICU project has truly excellent support for this kind of thing, although it is industrial strength ~ can be a bit heavyweight for personal use? They have all kinds of tools for externalizing and migrating/managing I18N concerns, and there's a D wrapper for ICU in the Mango library. > > I'm also interested in a lightweight implementation (Mango has a bunch of support functions to make it happen) ~ just haven't got around to hooking up the pieces yet. If ICU is not appropriate for your needs, and you think it's a reasonably idea, we might combine efforts? > Kris, may I send you the work I've done on this? So far I've got a general class to represent locales on the user's system, several classes for date/time, number and text formatting support, a set of calendars (Gregorian, Japanese, Hijri etc), and some string-related functions. The following example lists all the calendars supported by a locale. Culture[] allCultures = Culture.getCultures(CultureTypes.ALL); foreach (Culture culture; allCultures) { writefln("Culture: " ~ culture.name ~ " - " ~ culture.displayName); Calendar[] calendars = culture.optionalCalendars; writefln("Calendars:"); foreach (Calendar cal; calendars) { writefln("\t" ~ cal.toString()); } writefln(); } This example prints the days of the week in French: Culture culture = new Culture("fr-fr"); for (DayOfWeek dayOfWeek = DayOfWeek.SUNDAY; dayOfWeek <= DayOfWeek.SATURDAY; dayOfWeek++) { writefln(culture.dateTimeFormat.getDayName(dayOfWeek)); } John. |
Copyright © 1999-2021 by the D Language Foundation