How we can display floating or double numbers in C++ Builder? How we can apply formatting display to floating numbers? How we can use StrToFloatF method in C++ Builder? Can we use printf in Modern C++? Let’s answer all these questions.
Table of Contents
What does the phrase “formatted floating point numbers” mean in C++?
In programming, we use float, double, long double datatypes a lot for the floating numbers. When we use floating numbers sometimes we need to see them nicely laid out, so we can compare numbers next to it or with the number under or upper of that number. Sometimes they are long and we may need few precisions to display them. We can set the format of numbers in display, both its integer and precision side.
How we use formatting numbers in C++? How we can use fixed, scientific, or default formatting in C++? How we can use precision when formatting?
C++ Builder is a great programming language that has a lot of variations to help developer needs. We have explained how we can display floating numbers in C++ in this “Learn to Display Numbers With Formatting in C++” topic. C++ Builder supports CLANG compiler and all of these examples in that post can be applied.
In C++, the precision of floating numbers (float , double, long double) can be arranged by the std::precision() method We can fix the size of the display by using std::setw() method. In addition to these standard methods, we can use C++ Builder specific methods too. Let’s see these examples.
FloatToStrF()
FloatToStrF method converts a floating point Value
to a string, using a specified Format
, Precision
, and Digits
. FloatToStrF converts the floating-point value given by Value
to its string representation. Here is a full official format of this function.
- The
Value
parameter is the floating value to convert. - The
Format
parameter control how the value is formatted into a string. Format can be ffGeneral, ffExponent, ffFixed, ffNumber, ffCurrency. Please see the description of TFloatFormat for details.
ThePrecision
parameter specifies the precision of the given value. It should be 7 or less for values of type Single, 15 or less for values of type ‘Double, and 18 or less for values of type Extended. - The
Digits
parameter control how the digits of value is formatted into a string.
If the given value is a NAN (not-a-number), the resulting string is 'NAN'
. If the given value is positive infinity, the resulting string is 'INF'
. If the given value is negative infinity, the resulting string is '-INF'
.
Here is a simple example of formatting floating point numbers in C++
The first form of FloatToStrF is not thread-safe, because it uses localization information contained in global variables. The second form of FloatToStrF, which is thread-safe, refers to localization information contained in the FormatSettings parameter. Before calling the thread-safe form of FloatToStrF, you must populate FormatSettings with localization information. To populate FormatSettings with a set of default locale values, call TFormatSettings.Create.
Unicode printf() Method
While printf() is an old function, we can use new printf() method in Modern C++ applications. We can use standard printf() features on String or UnicodeString in a modern way. When using this printf method we should use L Literal format the string format before the “” as given example below.
printf() method here sets the value of a UnicodeString instance, given the format string and its arguments.
We can use printf to set the value of a UnicodeString instance, given a standard C++ format specifier. We can pass the values to any arguments in the format specifier as additional parameters following the format parameter. This method also returns the length, in characters, of the final formatted string.
We can also add ‘0’ for the empty spaces. This simple example shows the formatted string then its formatted length.
If you want to learn more or remember about all the details of printf() function, printf() Format Specifiers are described here and printf Precision Specifiers are described here in the official DocWiki of Embarcadero.
These methods can be used on C++ Builder Console Applications, Console VCL Applications, Console FMX Applications, and can be also used on C++ Builder VCL and FMX applications. For example, we can add these formatted floating numbers in UnicodeString form to any text property of any components of C++ Builder as the given example below,
RAD Studio, C++ Builder is a great programming language to calculate these kinds of problems in C++ and is also powerful enough for all your development needs. If you are new to C++ Builder, why not download and try C++ builder today?