Double-> How do I retain two decimal places when using string?

Source: Internet
Author: User
Double mynumber = 321.4321;
String mystring = string. Format ("{0: 0. ##}", mynumber );
Console. writeline (mystring );

String operation

Only two decimal places are allowed.

Double d1 = 3.1415926;
String strd1 = d1.tostring ("F2 ");
If there are three digits, It is F3.

String STR = "123abc456 ";
Int I = 3;
1. Take the first I character of the string.
STR = Str. substring (0, I); // or str = Str. Remove (I, str. Length-I );
2 remove the first I character of the string:
STR = Str. Remove (0, I); // or str = Str. substring (I );
3. Start from the right side with I characters:
STR = Str. substring (Str. Length-I); // or str = Str. Remove (0, str. Length-I );
4. Remove the I characters from the right:
STR = Str. substring (0, str. Length-I); // or str = Str. Remove (Str. Length-I, I );
5. If "ABC" exists in the string, remove it.
Using system. Text. regularexpressions;
String STR = "123abc456 ";
String A = "ABC ";
RegEx r = new RegEx ();
Match m = R. Match (STR );
If (M. Success)
{
// Select either the green or purple part.
STR = Str. Replace (,"");
Response. Write (STR );
String str1, str2;
Str1 = Str. substring (0, M. Index );
Str2 = Str. substring (M. index + A. length, str. Length-a.Length-m.Index );
Response. Write (str1 + str2 );
}
6. If the string contains "ABC", replace it with "ABC"
STR = Str. Replace ("ABC", "ABC ");



Public String strrep (string oldstr)
{

String newstr = oldstr. Replace ("<", "& lt ;");
Newstr = newstr. Replace (">", "& gt ;");
Newstr = newstr. Replace ("\ r \ n", "<br> ");
Newstr = newstr. Replace ("", "& nbsp ;");
Return newstr;
}






String temp = arr [0];
String [] S = temp. Split ('.');
String S1 = s [0];
String S2 = s [1];

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.