B
Bryan R. Meyer
I am a relatively new C++ programmer and am attempting to write a
function that takes a number of type float and adds commas to it in
the appropriate places. In order to manipulate the number to add the
commas, I convert it to a string (specifically a char[] rather than an
actual string object) using the
gcvt function as shown below.
char amt[50];
gcvt(amount,50,amt);
The problem I run into is a change in the precision. If amount is of
type float and has a value of 264588.44 then the code above results in
the string amt being "264588.4375". Note the change in the decimal
portion of the number; I don't want this. I want the original number
in the string. People have suggested using sprintf(). However,
sprintf() won't help me since I don't know the number of decimal
places in advance of the conversion.
Is there any way around this? Keep in mind that I don't know how many
decimal places I will need.
Thanks,
Bryan
function that takes a number of type float and adds commas to it in
the appropriate places. In order to manipulate the number to add the
commas, I convert it to a string (specifically a char[] rather than an
actual string object) using the
gcvt function as shown below.
char amt[50];
gcvt(amount,50,amt);
The problem I run into is a change in the precision. If amount is of
type float and has a value of 264588.44 then the code above results in
the string amt being "264588.4375". Note the change in the decimal
portion of the number; I don't want this. I want the original number
in the string. People have suggested using sprintf(). However,
sprintf() won't help me since I don't know the number of decimal
places in advance of the conversion.
Is there any way around this? Keep in mind that I don't know how many
decimal places I will need.
Thanks,
Bryan