How to add commas to large numbers?

Hey guys, coming from AS3, this would be relatively easy to do by breaking out the number string into an array and adding a comma every 3 numbers, but not sure how to do this with OF…

Basically I need 12345678 to look like 12,345,678 oh and it also needs to be aligned right not left.

ideas? suggestions? addons? Thanks for any help

I dunno if ofSplitString will work but you can try it. In my head :twisted: then you’ll have to reverse the string :wink:

Thanks! this is great, gonna give it another go this afternoon. hah I was actually looking through the docs and realized I didnt have the “show advanced” checked so completely missed “ofSplitString” - almost exactly like how I would do it in AS3

Thanks! this is great, gonna give it another go this afternoon. hah I was actually looking through the docs and realized I didnt have the “show advanced” checked so completely missed “ofSplitString” - almost exactly like how I would do it in AS3

So this is where I’m at right now, but it doesn’t seem to be working. Can anyone see what may be going wrong?

  
  
string m = ofToString(miles, 0);  
vector < string > m_split = ofSplitString(m, "");  
string compiled_miles = "";  
for(int i = (m_split.size()-1); i > 0; i--){  
	compiled_miles += m_split[i];  
	if(i%3==2&&i!=0) {  
		compiled_miles += ",";  
	}  
}  
odometerFont.drawString(compiled_miles, xPos, yPos);  

EDIT: it looks like you can’t split a string to individual characters by using “” is this a bug? or is there another way?

YES! just figured it out, for anyone thats curious:

  
  
string m = ofToString(miles,0);  
string compiled_miles = "";  
int counter = 1;  
for(int i=m.size(); i-->0;){  
	compiled_miles = char(m[i])+compiled_miles;  
	if((counter%3==0)&&(i!=0)) {  
		compiled_miles = ","+compiled_miles;  
	}  
	counter++;  
}  
  

for right alignment you can use “stringWidth” to adjust the xPosition.

1 Like

Great! glad you could make it :slight_smile: