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);
精彩评论