String Format for Double [C #]

Source: Internet
Author: User
Tags float number

Reprint: http://www.csharp-examples.net/string-format-double/

He following examples show how to format float numbers to string in C #. You can use the static method String.Format or instance methods double. ToString and float. Tostring.

Digits after decimal point

This example formats double to string with fixed number of decimal places. For the decimal places use pattern "0.00". If a float number has less decimal places, the rest digits on the right would be zeroes. If It has more decimal places, the number would be rounded.

[C #]

Just, places String.Format ("{0:0.00}", 123.4567);      //"123.46"String.Format (//"123.40"String.Format (//"123.00"      

Next example formats double to string with floating number of decimal places. e.g. for maximal the places use pattern "0.##".

[C #]

Max. Both decimal placesString.Format ("{0:0.##}", 123.4567);      //"123.46"String.Format (//"123.4"String.Format (//"123"      
Digits before decimal point

If you want a float number to has any minimal number of digits before decimal point use n-times Zero before de Cimal Point. e.g. pattern "00.0" formats a float number to string with at least, digits before decimal point and one Digi T after that.

[C #]

At least-digits before decimal pointString.Format ("{0:00.0}", 123.4567);      //"123.5"String.Format (//"23.5" String.Format (//"03.5"String.Format (//"-03.5" 
Thousands Separator

To format double to string with use of thousands separator use zero and comma separator before an usual float f Ormatting pattern, e.g. pattern "0,0.0" Formats the number to use thousands separators and to has one decimal Place.

[C #]

String.Format ("{0:0,0.0}", 12345.67);     //"12,345.7"String.Format ("{0:0,0}", 12345.67);       //"12,346"    
Zero

Float numbers between zero and one can be formatted in the ways, with or without leading zero before decimal poin T. To format number without a leading zero use # before point. For example '#.0' formats number to has one decimal place and zero-to-N digits before decimal point (e.g. ". 5" or "123.5").

Following code shows how can is formatted a zero (of double type).

[C #]

String.Format ("{0:0.0}", 0.0);            //"0.0"String.Format ("{0:0.#}", 0.0);            //"0"String.Format (//". 0"String.Format (         //""
Align numbers with spaces

To align float number to the right use comma "," option before the colon. Type comma followed by a number of spaces, e.g. '0,10:0.0' (this can is used only in String.Format method, not In Double. ToString method). To align numbers to the left use negative number of spaces.

[C #]

String.Format ("{0,10:0.0}", 123.4567);    //"     123.5"String.Format ("{0,-10:0.0}", 123.4567);   //"123.5"String.Format (//"-123.5"String.Format (     //" -123.5" 
Custom formatting for negative numbers and zero

If you need to use the custom format for negative float numbers or zero, use semicolon separator ";" to Spli T pattern to three sections. The first section formats positive numbers, the second sections formats negative numbers and the third section for Mats Zero. If you omit the last section, zero would be formatted using the first section.

[C #]

String.Format ("{0:0.00;minus 0.00;zero}", 123.4567);   //"123.46"String.Format ("{0:0.00;minus 0.00;zero}", -123.4567);  //"Minus 123.46"String.Format (//"zero"      
Some Funny Examples

As you could notice in the previous example, you can put any text to formatting pattern, e.g. before an usual pattern " c0>My text 0.0". Can even put any text between the zeroes, e.g. "0aaa. BBB0".

[C #]

String.Format ("{0:my number is 0.0}", 12.3);   //"My number is 12.3"String.Format ("{0:0aaa.bbb0}", 12.3);          //"12aaa.bbb3"    

String Format for Double [C #]

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.