How do I restrict output digits? For Example if

h is a float having value = 2.546709 and I want to output

it using cprintf, like cprintf (" blah %f", h); such that

only two digits show up on the screen.

h is a float having value = 2.546709 and I want to output

it using cprintf, like cprintf (" blah %f", h); such that

only two digits show up on the screen.

cout.precision(3) ;

You can also use a stream manipulator like

cout << setprecision(3) << somefloatingpointnumber << setprecision(6);

I have to look up the printf() stuff. It has been a while.

cprintf("blah %10.2f", h)

numbers not fitting in the format (eg. "%2f",100 -> 100) will be printed normal.

**************************

Format Specification Fields: printf and wprintf Functions

A format specification, which consists of optional and required fields, has the following form:

%[flags] [width] [.precision] [{h | l | I64 | L}]type

Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign is followed by a character that has no meaning as a format field, the character is copied to stdout. For example, to print a percent-sign character, use %%.

The optional fields, which appear before the type character, control other aspects of the formatting, as follows:

type Required character that determines whether the associated argument is interpreted as a character, a string, or a number (see Table R.3).

flags Optional character or characters that control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes (see Table R.4). More than one flag can appear in a format specification.

width Optional number that specifies the minimum number of characters output. (See printf Width Specification.)

precision Optional number that specifies the maximum number of characters printed for all or part of the output field, or the minimum number of digits printed for integer values (see Table R.5).

h | l | I64 | L Optional prefixes to type-that specify the size of argument (see Table R.6).

**************************

Table R.5 How Precision Values Affect Type

Type

Meaning

Default

c, C

The precision has no effect.

Character is printed.

d, i, u, o, x, X

The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision.

Default precision is 1.

e, E

The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded.

Default precision is 6; if precision is 0 or the period (.) appears without a number following it, no decimal point is printed.

f

The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.

Default precision is 6; if precision is 0, or if the period (.) appears without a number following it, no decimal point is printed.

g, G

The precision specifies the maximum number of significant digits printed.

Six significant digits are printed, with any trailing zeros truncated.

s, S

The precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed.

Characters are printed until a null character is encountered.

**************************

char NumStr[5];

float h = 2.2556632;

sprintf(NumStr,"%0.2f",h);

read about sprintf() (and printf()) and let me know if you have questions.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.