开发者

Getting the decimals in a double variable in a culture sensitive way

Assume this double variable:

double d = 59.123;

How could I get the fir开发者_如何学运维st 2 decimals in a string and in a culture sensitive way? That is:

string s = "12";


You can substract its integer part, multiply by 100, truncate decimal part and cast to string.

private string GetDecimalDigits(double d, int digitsCount)
{
    double substracted = d - Math.Floor(d);
    return Math.Round(substracted * Math.Pow(10, digitsCount)).ToString();
}

string result = GetDecimalDigits(59.123, 2);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜