For the use of & quot; R & quot; (round-trip program format) in the value format string

Source: Internet
Author: User

For the use of the "R" (round-trip program format) format in the numeric format string, the string Program

There are many formatting formats for numeric string formatting, such as "C" for currency format and "P" for percentage format. FCL supports multiple formatting methods. Sometimes we convert a value to the string type and then from the string type to the numeric type. At this time, we must consider whether the converted value will be equal to the original one?

The first case is:

In general G format

Using System; namespace CharStringAndSText {class Program {static void Main (string [] args) {// original string double originDouble = 0.12314353454478; // 14-bit float originFloat = 1.222223f; // 6-bit precision // string after conversion; string originDoubleToString = originDouble. toString ("G"); string originFloatToString = originFloat. toString ("G"); // after being converted to a value, double backToDouble = Double. parse (originDoubleToString); float backToFloat = Single. parse (originFloatToString); // result: bool resultDouble = backToDouble. equals (originDouble); bool resultFloat = backToFloat. equals (originFloat); // print the Console. writeLine ("ResultDouble is" + resultDouble. toString () + "with G format"); // print the result: True Console. writeLine ("ResultFloat is" + resultFloat. toString () + "with G format"); // print the result: True Console. readKey ();}}}

When we add the precision of double to 15 digits and float to 7 digits

Using System; namespace CharStringAndSText {class Program {static void Main (string [] args) {// original string double originDouble = 0.123143534544786; // 15-bit float originFloat = 1.2222234f; // 7-bit precision // string after conversion; string originDoubleToString = originDouble. toString ("G"); string originFloatToString = originFloat. toString ("G"); // after being converted to a value, double backToDouble = Double. parse (originDoubleToString); float backToFloat = Single. parse (originFloatToString); // result: bool resultDouble = backToDouble. equals (originDouble); bool resultFloat = backToFloat. equals (originFloat); // print the Console. writeLine ("ResultDouble is" + resultDouble. toString () + "with G format"); // print the result: True Console. writeLine ("ResultFloat is" + resultFloat. toString () + "with G format"); // print the result: False Console. readKey ();}}}

The above results are displayed on my computer. We can see that the float value is different from the original one. When the double type exceeds 15 bits, the double type is usually not equal. Therefore, you can use the R format to format it.

Using System; namespace CharStringAndSText {class Program {static void Main (string [] args) {// original string double originDouble = 0.123143534544786122; // 18-bit float originFloat = 1.2222234223f; // 10-bit precision // string after conversion; string originDoubleToString = originDouble. toString ("R"); string originFloatToString = originFloat. toString ("R"); // after being converted to a value, double backToDouble = Double. parse (originDoubleToString); float backToFloat = Single. parse (originFloatToString); // result: bool resultDouble = backToDouble. equals (originDouble); bool resultFloat = backToFloat. equals (originFloat); // print the Console. writeLine ("ResultDouble is" + resultDouble. toString () + "with G format"); // print the result: True Console. writeLine ("ResultFloat is" + resultFloat. toString () + "with G format"); // print the result: True Console. readKey ();}}}

This is the use of the back-to-back format. However, when the Code Compiled by a 32-bit computer runs on a 64-bit computer, R cannot achieve perfect conversion. At this time, G17 must be used for formatting.

When formatting a string based on the R symbol written in the document, we first use the regular format to test the string. The Double type uses the accuracy of 15, and the float uses the 7-bit precision, if the returned value is the same as the original value, G is used. If it fails, Double uses the 17-bit precision and Single uses the 9-bit precision. Only the Double and Float types can use the R format characters.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.