Issues related to ToString ("C2") when transferring RMB amount

Source: Internet
Author: User

We often encounter the conversion of a number to an amount string, which is simple, such as:

1 class Program2     {3         Static voidMain (string[] args)4         {5             Doublenum =123.456;6             stringNUM1 = num. ToString ("C2");7 Console.WriteLine (NUM1);8 Console.read ();9         }Ten}
View Code

where "C" is the amount of the transfer, "2" is the precision that represents the two digits after the decimal point is reserved.

The result is

However, the actual process we write, the customer encountered a problem, that is, in their, with the same code, the result is:

Finally, it is found that the ToString () method has an overload of two parameters, which is given by the official:

1 //2         //Summary:3         //converts the numeric value of this instance to its equivalent string representation, using the specified format and culture-specific format information. 4         //5         //Parameters:6         //format:7         //a numeric format string. 8         //9         //provider:Ten         //An object that provides culture-specific formatting information.  One         // A         //return Result: -         //The string representation of the value of this instance, as specified by format and provider.  - [SecuritySafeCritical] the          Public stringToString (stringFormat, IFormatProvider provider);
View Code

So, if we give only one parameter, the second parameter takes the local default value, and this value is related to some parameter settings in the operating system. If we can give the second parameter, we can guarantee to convert to RMB in any operating system, the improved code is

1 class Program2     {3         Static voidMain (string[] args)4         {5             Doublenum =123.456;6             stringNUM1 = num. ToString ("C2", CultureInfo.CreateSpecificCulture ("ZH-CN")));7 Console.WriteLine (NUM1);8 Console.read ();9         }Ten}
View Code

Here are a few common currency symbol codes:

RMB: ZH-CN

USD: en-US

Franc: Fr-fr

JPY: JA-JP

Note: The yen is a sheep character a cross, the renminbi is two horizontal: ¥

Reference Document: http://msdn.microsoft.com/zh-cn/dynamics/dwhawy9k (v=vs.85)/

Issues related to ToString ("C2") when transferring RMB amount

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.