fixed() function in C++
In this article, we will discuss the fixed() function in C++ with its syntax, examples, and benefits.
What is fixed() function?
Fixed() function establishes a fixed floating-point output format, such as decimal notation. It is not a standard library function in C++. You can refer to a custom or function from an outside library.
However, a data type or variable with a fixed-point representation is frequently referred to as "fixed" in general programming terminology. A numerical model with a fixed number of digits following the decimal point is called a fixed-point number. It is frequently applied when working with decimal data to prevent possible rounding problems that may occur when using floating-point numbers. Sets the floatfield format flag for the str stream to be fixed. When the floatfield value is set to fixed, floating-point values are expressed using fixed-point notation: the value is defined with the precise number of decimal digits indicated by the precision field (precision), and there is no exponent portion.
flag value | C++98 | C++11 |
Fixed | Write fixed-point notation for floating-point values. | Write fixed-point notation for floating-point values. |
Scientific | Write scientific notation for floating-point values. | Write scientific notation for floating-point values. |
hexfloat | - | Hexadecimalize floating-point values before writing them. This has the same value as (fixed|scientific). |
defaultfloat | - | Use the standard floating-point notation when writing floating-point values. Without any other floatfield bits being set, this value is used by default. |
Note:
Be aware that the precision field is handled differently in the fixed and scientific notations compared to the usual floating-point notation. In the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if they follow decimal zeros. On the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display before and after the decimal point.
Let us see an example program for a clear understanding:
Example 1:
#include <iostream>
using namespace std;
int main () {
//assigning values
double t = 24.90876;
double a = 1887.0;
double e = 2.3e-5;
//precession
cout.precision(5);
//default output
cout << "default:\n";
cout << t << '\n' << a << '\n' << e << '\n';
cout << '\n';
//fixed output
cout<< "fixed:\n" <<fixed;
cout<< t << '\n' << a << '\n' << e << '\n';
cout << '\n';
//scientific output
cout << "scientific:\n" <<scientific;
cout << t << '\n' << a << '\n' << e << '\n';
return 0;
}
Output:
![fixed() function in C++](https://static.tutorialandexample.com/cpp/fixed-function-in-cpp1.png)
Example 2:
#include<iostream>
using namespace std;
int main(){
// Initializing floating point variable
double a=410.3576392;
double b=100.0;
// Specifying precision
cout.precision(4);
// Printing normal values
cout<<"Normal values : a="<<a<<" "<<"b="<<b<<endl;
// Printing values using fixed
cout<<"Values using fixed"<<fixed<<endl;;
cout<<"a="<<a<<" "<<"b="<<b<<endl;
return 0;
}
Output:
![fixed() function in C++](https://static.tutorialandexample.com/cpp/fixed-function-in-cpp2.png)
Other Benefits of fixed() function in C++:
There is no necessity for the "Fixed()" function in C++ because it is not a standard function. However, there could be several reasons why a programmer adds a particular "Fixed()" method to their C++ codebase or comes across it in a third-party library. Several situations where such a function might be helpful are listed below:
Custom data type or class: A custom data type or class member function may be the "fixed()" function. Use this function to manipulate or carry out actions particular to that data type. For instance, the "fixed()" function can convert a floating-point number to a fixed-point representation if you have a custom class that represents a fixed-point number.
External library or framework: Using a library or framework from a different company can include a "fixed()" function in its API. In this scenario, the function's objective and functionality would be unique to that library or framework. Therefore, you would need to refer to its documentation for more details.
Data formatting: Fixed-length or fixed-format data can be formatted or handled using the "fixed()" function. For instance, the "fixed()" method might take care of duties like adding or deleting the string to a fixed length if you had a string with a particular format that needed to be fixed or normalized.
Fixed-point computation: The "Fixed()" function might be created to perform fixed-point computations. With a predetermined number of digits following the decimal point, fixed-point arithmetic is used to represent decimal numbers, giving exact control over the precision and range of the numbers. The function may be used for fixed-point numbers to perform addition, subtraction, multiplication, and division operations.