Decimal separator
nrgyzer
nrgyzer at gmail.com
Sat Feb 13 08:02:59 PST 2010
BCS Wrote:
> Hello Nrgyzer,
>
> > Hello everyone,
> >
> > I'm trying to split an integer into a integer with decimal points (or
> > char[] with decimal points). I already know that I can do this with
> > std.format.doFormat() but I don't know how I can use this to convert
> > my int (for example... 10000) to 10,000.
> >
> > I hope anyone can help me :)
> >
> > In this case... thanks so much for help
> >
>
> char[] AddCharEvery(char[] str, char c, int i)
> {
> if(str.length > i) return AddCharEvery(str[0..$-i],c,i) ~ c ~ str[0..$-i];
> else return str;
> }
>
> or
>
>
> char[] AddCharEvery(char[] str, char c, int i)
> {
> if(str.length <= i) return str;
>
> auto ret = new char[str.length + (str.length-1)/i];
>
> int at = ret.length;
> while(str.length > i)
> {
> ret[at-i..at] = str[$-i..$];
> ret[at-i-1] = c;
> at -= (i+1);
> str = str[0..$-i];
> }
> ret[0..at] = str;
> return ret;
> }
>
> //example
> import std.stdio;
> void main()
> {
> writef("%s\n", AddCharEvery("100000000000000", ',', 3));
> writef("%s\n", AddCharEvery("10000000000000", ',', 3));
> writef("%s\n", AddCharEvery("1000000000000", ',', 3));
> writef("%s\n", AddCharEvery("100000000000", ',', 3));
> }
>
>
> --
> <IXOYE><
>
>
Thanks... that's exactly what I need :)
More information about the Digitalmars-d
mailing list