C 小數點格式化用法小結

2021-07-16 22:55:48 字數 3395 閱讀 8728

1.tostring()方法?

12

3

doubled=12345678.2334;

console.writeline(d.tostring("f2"));

console.writeline(d.tostring("###,###.00"));//12,345,678.23

2.math.round()方法?

12

3

4

5

6

7

8

9

10

math.round(3.44, 1);//returns 3.4.

math.round(3.45, 1);//returns 3.4.

math.round(3.46, 1);//returns 3.5.

math.round(3.445, 1);//returns 3.4.

math.round(3.455, 1);//returns 3.5.

math.round(3.465, 1);//returns 3.5.

math.round(3.450, 1);//returns 3.4.(補0是無效的)

math.round(3.4452, 2);//returns 3.45.

math.round(3.4552, 2);//returns 3.46.

math.round(3.4652, 2);//returns 3.47.

"四捨六入五考慮,五後非零就進一,五後皆零看奇偶,五前為偶應捨 去,五前為奇要進一"

短一點的口訣叫「四捨、六入、五湊偶」

3.double.parse()方法?

12

doubled=1.12345;

d=double.parse(d.tostring("0.00"));

4.輸出百分號?

12

3

4

5

6

7

system.globalization.numberformatinfo provider =newsystem.globalization.numberformatinfo();

provider.percentdecimaldigits = 2;//小數點保留幾位數.

provider.percentpositivepattern = 1;//百分號出現在何處.

doubleresult = (double)1 / 3;//一定要用double型別.

console.writeline(result.tostring("p", provider));

//或

console.writeline((result*100).tostring("#0.#0")+"%");

5.string.format()方法?

12

3

4

5

6

7

stringstr1 = string.format("",56789);//result: 56,789.0

stringstr2 = string.format("",56789);//result: 56,789.00

stringstr3 = string.format("",56789);//result: 56,789.000

stringstr8 = string.format("",56789);//result: 56789.0

stringstr9 = string.format("",56789);//result: 56789.00

stringstr11 =(56789 / 100.0).tostring("#.##");//result: 567.89

stringstr12 =(56789 / 100).tostring("#.##");//result: 567

C 小數點格式化

1.tostring 方法 double d 12345678.2334 console.writeline d.tostring f2 1234.23 console.writeline d.tostring 00 12,345,678.23 2.math.round 方法 math.round ...

C 小數點格式化

1.tostring 方法 double d 12345678.2334 console.writeline d.tostring f2 console.writeline d.tostring 00 12,345,678.23 2.math.round 方法 math.round 3.44,1 r...

C 小數點格式化

1.tostring 方法 double d 12345678.2334 console.writeline d.tostring f2 console.writeline d.tostring 00 12,345,678.23 2.math.round 方法 math.round 3.44,1 r...