Connecting Tech Pros Worldwide Forums | Help | Site Map

sprintf for %f

Newbie
 
Join Date: Oct 2007
Posts: 3
#1: Oct 5 '07
Hi,

I am new to C++. I need to format a float variable to 4d.9d ( Two numbers before decimal and 8 after decimal ).

How can I use sprintf for this?

Hunderpanzer's Avatar
Member
 
Join Date: Jul 2007
Posts: 61
#2: Oct 5 '07

re: sprintf for %f


Quote:

Originally Posted by suunknown

Hi,

I am new to C++. I need to format a float variable to 4d.9d ( Two numbers before decimal and 8 after decimal ).

How can I use sprintf for this?


Sorry I don't know much about that, but I found this for you:


http://www.cplusplus.com/reference/c...o/sprintf.html

If this doesn't help, I'm sure someone will come and give you better advice.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,382
#3: Oct 5 '07

re: sprintf for %f


Read page 243 in The ANSI C Programming Language by K&R.

Afteer the % you can specify your field.

A number is the field width. Use a negative number for left-justify.
A period follows the field width by the precision. In the case of a float this will be the number of decimals to the right of the decimal point.

So, to do 4d.9d on a float you would:

%13.9f

and it still won't work!

You see, a float has only six significant digits. You need to use a double.
Newbie
 
Join Date: Oct 2007
Posts: 3
#4: Oct 6 '07

re: sprintf for %f


Hi,

Thanks for the reply.

I am using double only. I need total lenght of 14.

The decimal places can be from 0-12.

For ex: the number can be 12.123456789012 or it can be without decimal places like 1234567890123

Total length cannot exceed 14.

I have to convert that using sprintf to char's

I am using:
sprintf(buffer, '%14f', value );

By using so, when I have decimal places greater that 6, it is rounding to 6 decimal places.

What can I use in this case?
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,382
#5: Oct 6 '07

re: sprintf for %f


You must have misunderstood my post.

Expand|Select|Wrap|Line Numbers
  1. printf("%14.9f\n",12.123456789012);
  2.  
This says in a field of 14, right-justify this double and show 9 decimal places.
Newbie
 
Join Date: Oct 2007
Posts: 3
#6: Oct 7 '07

re: sprintf for %f


Thanks again for your reply.

But I donnot know variable precision or scalar length upfront.

It can be in the following way

1) 1234.123456789 (total of 14 chars )

or it can be

2) 123456789.1234 ( total 14 chars )

or it can be

3) 123456.1234567 ( total 14 chars )

It should be 14chars length (scalar & precision will vary). In this case how I should have it in sprintf.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,382
#7: Oct 8 '07

re: sprintf for %f


Quote:

Originally Posted by suunknown

It should be 14chars length (scalar & precision will vary). In this case how I should have it in sprintf.

Are you reading my posts??

1234.123456789 is "%14.9f"
123456789.1234 is "%14.4f"
123456.1234567 is "%14.7f"

Get a copy of of the ANSI C Programming Language by K&R.

This has all been clearly documented.
Reply