digitalmars.D - Decimal separator
- nrgyzer (4/4) Feb 12 2010 Hello everyone,
- Justin Whear (2/5) Feb 12 2010 Well, integers don't have anything following the decimal, so there's not...
- BCS (33/44) Feb 12 2010 char[] AddCharEvery(char[] str, char c, int i)
- nrgyzer (2/58) Feb 13 2010 Thanks... that's exactly what I need :)
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
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
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 helpchar[] 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
BCS Wrote:Hello Nrgyzer,Thanks... that's exactly what I need :)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 helpchar[] 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 13 2010