using setprecision without fixed or scientific format. – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#4326    siteicon   siteicon  

using setprecision without fixed or scientific format.

setprecision to set number of digits for any decimal containing floating number.

Learning Objectives

  • Effect of using setprecision on floating numbers in float/double format.

Source Code

TC++ #4326


Source Code

Run Output


Output in TurboC++ DOS

This is because setprecision works for digits after decimal only for TurboC type of compiler.

Code Understanding

double d=123.5513; 
This number will be used to apply precision formatting on. It has 3 digits before decimal and 4 digits after decimal. Total 7 digits.

This should print 123.551 as default precision for a floating number is 6 digits. If the last digit would be 6 or more than the rounding off previous digit would have happened to higher number.

Here we are setting precision of 4 digits so the only 4 digits in a most significant way would be printed. This will print 123.6 as 4 digits are to be printed and after decimal rounding off rules will apply for any subsequent digits of 5 or more.

Here all the 7 digits will print as 123.5513  as the original number has 7 digits and precision is also set to 7.

Here again all the digits will print as it is as precision is set to more than required 7 digits. So output would be 123.5513

Here 124 will print as rounding off rules will apply and 5 being immediately after decimal will effect the number 3 just before decimal to become 4 due to rounding off.

Since in this case the precision is less than the non decimal part, so the number will attempt to print itself to a nearest scientific format number as 1.2e+02 which means 1.2 multiplied by 10 raised to power two which is basically equivalent to 120. Had this been then the output would have been 1.3e+02 which is equivalent to 130. Again the effect of rounding off can be noticed.

Note: Above explanation has been given for modern compilers and not for things like Turboc++ DOS version which takes into account the digits after decimal only and thus the output is different as shown in the output section.


Suggested Filename(s): setprecision.cpp


sunmitra| Created: 18-May-2018 | Updated: 28-Aug-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues