www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - number formatting

reply "John C" <johnch_atms hotmail.com> writes:
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. 
Jan 12 2006
next sibling parent reply Carlos Santander <csantander619 gmail.com> writes:
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
Jan 12 2006
parent reply "John C" <johnch_atms hotmail.com> writes:
"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"?
Jan 12 2006
parent Carlos Santander <csantander619 gmail.com> writes:
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
Jan 12 2006
prev sibling parent reply HienTau <hientau.mail gmail.com> writes:
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
Jan 13 2006
parent reply "John C" <johnch_atms hotmail.com> writes:
"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.
Jan 13 2006
next sibling parent HienTau <hientau.mail gmail.com> writes:
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 ;-)
Jan 13 2006
prev sibling parent reply "Kris" <fu bar.com> writes:
"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?
Jan 13 2006
next sibling parent "John C" <johnch_atms hotmail.com> writes:
"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.
Jan 13 2006
prev sibling parent reply "John C" <johnch_atms hotmail.com> writes:
"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.
Jan 16 2006
next sibling parent "Kris" <fu bar.com> writes:
Please do!

use this account:  someidiot at at at earthlink dot . dot net


"John C" <johnch_atms hotmail.com> wrote in message 
news:dqghld$1llj$1 digitaldaemon.com...
 "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.
Jan 16 2006
prev sibling parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
John C wrote:
 "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)); }
Nice. But: what about locales where Monday is the first day of week?
Jan 16 2006
parent "John C" <johnch_atms hotmail.com> writes:
"Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message 
news:dqhavm$2fmg$1 digitaldaemon.com...
 John C wrote:
 "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)); }
Nice. But: what about locales where Monday is the first day of week?
You'd offset it with the 'firstDayOfWeek' property.
Jan 16 2006