Site icon Learn C++

How To Convert A Float To A String In A C++ App

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++.

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:

[crayon-67633afc609a7903932453/]

Is there a simple example of the FloatToStr() method?

Simply we can convert a floating number to a string as below,

[crayon-67633afc609b6021423360/]

FloatToStr() method can be used with double and long double floating numbers too. For example;

[crayon-67633afc609b7845534466/]

in the latest RAD Studio versions this is same as below,

[crayon-67633afc609b8336188223/]

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,

[crayon-67633afc609bd732132083/]

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,

[crayon-67633afc609be967209831/]

What is the FloatToStrF() Method in C++?

FloatToStrF method converts a floating point Value to a string, using a specified FormatPrecision, and Digits. FloatToStrF converts the floating-point value given by Value to its string representation. Here is a full official format of this function.

[crayon-67633afc609bf498890400/]

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

[crayon-67633afc609c0810945717/]

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.

[crayon-67633afc609c3895753765/]

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.

[crayon-67633afc609c5663705983/]

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,

[crayon-67633afc609c8141353968/]

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.

Exit mobile version