Comma in numbers?

I'm writing a program, that generates a number, and prints it to the Serial Monitor.

The number is displayed as... Total Number Of Winds = 17040.

I'd like to have the number have a comma in it so it looks like 17,040.

I've tried everything I can fined, but I'm new to programing.

This is the important bits of the code, for this question.

float totalnumofcoils; // calculated

void setup() {
Serial.begin(9600);

totalnumofcoils = (totallengthft * 12) / avgwraplengthin; // calculation part

Serial.print("Total Number Of Winds = ");

Serial.println(totalnumofcoils,0); // prints 17040 and I want it to be 17,040
}

Serial.println(totalnumofcoils,0); // prints 17040 and I want it to be 17,040

It does not make sense to compute the value as a float when you want an integer value to be printed.

The Print class, that Serial derives from, does not do much formatting. sprintf() does. You can then print the string that it produces.

So are you saying I need to write it as int totalnumofcoils;// calculated ?

I don't understand the use of sprintf() . Is that part of Software.Serial.h library?

Can you be more specific as to how to use sprintf()?

So are you saying I need to write it as int totalnumofcoils;// calculated ?

That makes more sense that does ignoring the fractional part of a float.

I don't understand the use of sprintf() . Is that part of Software.Serial.h library?

No, and I'm sure didn't suggest that it was.

Can you be more specific as to how to use sprintf()?

No, that is YOUR part of the project. I'm not certain that sprintf() can format the number the way you want. But, once you have a string, modifying the string to add a comma n places from the end is child's play.

robinhosler:
I'm writing a program, that generates a number, and prints it to the Serial Monitor.

The number is displayed as... Total Number Of Winds = 17040.

I'd like to have the number have a comma in it so it looks like 17,040.

I've tried everything I can fined, but I'm new to programing.

You want a "thousands seperator" and not a "decimal komma", correct?

You can decide whether the number is 1000 or more ==> format using thousands seperator ','
Or if number is less than 1000 ==> normal formatting

Formatting depends on the actual number format.
Here is an example if "winds" is a "long":

 long winds=17040;
 char buf[21];
 if (winds>=1000)
   snprintf(buf,sizeof(buf),"%ld,%03ld",winds/1000,winds%1000);
 else  
   snprintf(buf,sizeof(buf),"%ld",winds);
 Serial.println(buf);

Edit: Code example works for positive numbers only. Do you need negative numbers, too?

Thank you. You are correct on all accounts.

I programmed, in BASIC, back in the 80's, and I'm learning the Arduino code. So I have a clue, but a lot of my "old" knowledge is hard to retrieve from my brain.

I'll try this, as I understand it. lol

Thanks, again jurs.

Worked like a charm. Thanks again jurs.

Here is the results:

ENTER Desired Coil Resistance ..... = 7.50

ENTER Wire Gauge .................. = 40
ENTER Pole Diameter .............. = 0.2500
ENTER Outer Pole Centers ......... = 2.1250

Diameter Of Wire ................. = 0.0031
Resistance Per 1000ft. ........... = 1049
Apx. Total Length / Ft. .......... = 7150
Avg. Length Of Wrap / In. ........ = 5.035

Total Number Of Winds ............ = 17,039

How come this one:

Apx. Total Length / Ft. .......... = 7150

doesn't have a comma?

Because, in the end, only the four lines of input, and Total Number Of Winds data will be displayed. The rest of the data is only being displayed temporarily, so I can check it's accuracy.

The program is going to run a pickup coil winder, to make guitar pickups.

Thanks all.

Your post would have made more sense to me if you had used the term: "turns", instead of "winds". I thought you were passing gas while programming.

Paul