www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Decimal separator

reply nrgyzer <nrgyzer gmail.com> writes:
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
Feb 12 2010
next sibling parent Justin Whear <justin economicmodeling.com> writes:
nrgyzer Wrote:

 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.

Well, integers don't have anything following the decimal, so there's nothing to do really. Unless you want to convert the integer to a string and append ".00" or something. Perhaps you want to convert the integer to a string and insert thousands separators? If so, you'll want to loop from the end of the string towards the beginning, inserting a comma every 3 characters.
Feb 12 2010
prev sibling parent reply BCS <none anon.com> writes:
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><
Feb 12 2010
parent nrgyzer <nrgyzer gmail.com> writes:
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 :)
Feb 13 2010