Solved

Restricting Digits

Posted on 1998-07-31
16
372 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:varun071898
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 3
  • 3
  • +1
16 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1169138
you can use the precision() function.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169139
Like

cout.precision(3) ;

You can also use a stream manipulator like

cout << setprecision(3)  << somefloatingpointnumber << setprecision(6);
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169140
Note that there are two interpretations to this precision.  If the stream is in fixed or scientific format the precison is the number of decimal digits to be displayed.  If the stream is in the automatic format, the precision is the total number of digits to be displayed.  it sounds like you want the fixed or scientific format, but I can't be sure.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:varun071898
ID: 1169141
I am using a very old version of C++. What I want is a operator like cprintf ("blah %f", h) I have used it before and have forgotten, it was something like cprintf ("blah %f2", h) something like that. I want that answer.

ps. Precision is not available.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169142
precision is for the stream output.  And you clearly stated you wanted printf() output.  Well you get what you pay for.  Bad answers at a good price.

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

Expert Comment

by:fkurucz
ID: 1169143
to show h with 10 digits (leading spaces) and 2 decimals use
    cprintf("blah %10.2f", h)
numbers not fitting in the format (eg. "%2f",100 -> 100) will be printed normal.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1169144
From VC's help I found
*********************************************
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.
*****************************************

0
 
LVL 22

Expert Comment

by:nietod
ID: 1169145
Thus you should need

cprintf (" blah %0.2f", h)

to get 2 decimals
0
 
LVL 11

Expert Comment

by:alexo
ID: 1169146
Now who sounds like a textbook?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169147
hey, at least I provide a reference.  you just paste it without a word.  That's plagurism (or something like that, but spelled differently.)
0
 
LVL 11

Expert Comment

by:alexo
ID: 1169148
>> That's plagurism
The plagiarism of a guru?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169149
Great, my English is being corrected by someone who speaks it as a third language.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1169150
et list yor ansers ar korekt ;-)
0
 
LVL 3

Author Comment

by:varun071898
ID: 1169151
Hi Nietod,
I will be increasing the points to 50 and give you A: Excellent!
but please answer one more question. How do I want to have a floating point number to be changed into a character type. For ex:
float h=2.2556632
then char ch = 2.25. How do I do this. You can tell me how to do this or give the code. I want this urgently. Thanks

0
 
LVL 22

Expert Comment

by:nietod
ID: 1169152
I think what you mean is you want to convert the floating point number to a character string that expresses the floating point number, like convert 2.25 to the 4 character string (plus NULL terminator) "2.25"  Is that right?  If so, you can use the sprintf() function.  There is a lot of things that can be done with the sprintf() function.  (More than I can explain here, so you need to look it up.)  But for your purposes you would do

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.
0
 
LVL 3

Author Comment

by:varun071898
ID: 1169153
Thanks nietod. As promised I have increased the points to 50 and given you A:Excellent
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question