How can I convert a floating number to a string in my C++ app? How can I put a float into a Text of a component? What is FloatToStr method? What is FloatToStrF method? Can I use FloatToStr method with double and long double too? Can we use printf() method with float in modern C++? Let’s answer these questions.
Modern C++ uses Unicode string format and visual components of the C++ Builder have a lot of text properties which are String means they are UnicodeString. So, generally, if you want to see an integer or float number in these modern texts, you must convert your number value to a text value, to a String. C++ Builder has a lot of specific methods in its libraries, and the VCL and FMX libraries have a lot of specific methods that can be used easily in your C++ applications. We can easily convert float, double and long double floating numbers to a modern Unicode string with FloatToStr, FloatToStrF or printf method of UnicodeStrings in modern C++.
Table of Contents
What are Floating Numbers in a C++ app?
In C++ programming, we use float, double, long double datatypes a lot for the floating numbers. When we use floating numbers, we sometimes need to convert or 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 a few precisions to display them. We can set the format of numbers in display, both its integer and precision side.
How do 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.
What does the String and UnicodeString mean?
In the latest versions of C++ Builder (10 and above), Strings are Unicode Strings. Unicode standard for UnicodeString provides a unique number for every character (8, 16 or 32 bits) more than ASCII (8 bits) characters. UnicodeStrings are being used widely because of support to languages worldwide and emojis. In modern C++ nowadays there are two types of strings used; the array of chars (char strings) and UnicodeStrings (WideStrings and AnsiStrings are older, not compatible with all features now). CLANG / C++ Builder / GNU C / VC++ compilers, IDEs are using this standard for GUI forms to support all languages to provide applications in global. More information about the structure of Unicode Strings can be found here. RAD Studio, Delphi & C++ Builder uses Unicode-based strings: that is, the type String is a Unicode string (System.UnicodeString) instead of an ANSI string. If you want to transform your codes to Unicode strings, we recommend this article.
Here is a post about how to use Unicode Strings
How can we convert a floating number to a String?
Simply we can use FloatToStr() method to convert a floating number to a string. Here is the syntax of FloatToStr() method,
Syntax:
1 2 3 |
UnicodeString __fastcall FloatToStr(System::Extended Value); |
Is there a simple example of the FloatToStr() method?
Simply we can convert a floating number to a string as below,
1 2 3 4 |
float g = 9.81; String str = FloatToStr( g); |
FloatToStr() method can be used with double and long double floating numbers too. For example;
1 2 3 4 |
double g = 9.81; String str = FloatToStr( g); |
in the latest RAD Studio versions this is same as below,
1 2 3 4 |
double g = 9.81; UnicodeString str = FloatToStr( g); |
FloatToStr method can be directly used to set Text properties (which are UnicodeString) of a component. For example we can put floating number to Text property of Edit (TEdit) component as below,
1 2 3 4 |
float g = 9.81; Edit1->Text = FloatToStr( g); |
Here is a full example of the FloatToStr() method
We can use FloatStr method in our VCL and FMX applications. Here is a FMX example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <fmx.h> #pragma hdrstop #include "FloatToStr_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { long double g= 9.81; String str; str = FloatToStr(g); ShowMessage(str); } |
What is the FloatToStrF() Method in C++?
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.
1 |
<strong>extern</strong> DELPHI_PACKAGE System::UnicodeString <strong>__fastcall</strong> FloatToStrF(System::Extended Value, TFloatFormat Format, int Precision, int Digits)<em>/* overload */</em>; |
- 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 a C++ app
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <vcl.h> int _tmain(int argc, _TCHAR* argv[]) { float pi = M_PI; String str = FloatToStrF(pi, ffFixed, 8,5); ShowMessage(str); return 0; } |
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.
How can we use the Unicode printf() method with floating numbers?
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <vcl.h> int _tmain(int argc, _TCHAR* argv[]) { String str ; float x = 123456.123456789; str.printf(L"%8.3f", x); ShowMessage(str); return 0; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <vcl.h> int _tmain(int argc, _TCHAR* argv[]) { double x = 123456.123456789; String str; int len=str.printf(L"%08.3f", x); ShowMessage(str); ShowMessage(len); return 0; } |
If you want to learn more or remind yourself 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,
1 2 3 4 5 6 7 8 9 10 11 12 |
double x = 123456.123456789; UnicodeString str; int len=str.printf(L"%08.3f", x); Memo1->Lines->Add(str); Edit1->Text = str; Label1->Text = str; Form1->Caption =str; |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.