开发者

Cutting down output after bytes to gigabytes conversion

I'm converting bytes into gigabytes and when I do, the output is something like:

57.686961286

Is there any way I can cut down the total number displayed to something开发者_如何转开发 like:

57.6?


Yes, you could use the Math.Round method, which will round a value to the nearest integer or the specified number of decimal places.

The appropriate overload will be chosen depending on the type of the value that you pass in (either a Double or a Decimal). Specifying an Integer value for the second parameter allows you to indicate the number of fractional digits the result should contain. In this case, you would specify "1".

Of course, the result would not be 57.6. When the value 57.686… is rounded, the 8 in the hundredths place causes the 6 in the tenths place to round up to 7, rather than down to 6. The correct result is 57.7.

Certain overloads of the above method also allow you to specify a rounding style to apply when the number in question is halfway between two others, either IEEE Standard 754 section 4 rounding (also called rounding-to-nearest, or "banker's" rounding), or the "away from zero" style that you probably learned in school.


You can format the value for display using the ToString() method, which takes a format parameter.

double myValue = 57.686961286;
string outputValue = myValue.ToString("0.0"); //output: 57.7, rounded
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜