private void button_Click(object sender, EventArgs e)
{
Debug.WriteLine(calc_significant_figures(1040000.00M, 2, 0)); // -> 1000000
Debug.WriteLine(calc_significant_figures(1050000.00M, 2, 0)); // -> 1100000
Debug.WriteLine(calc_significant_figures(10.0000400M, 6, 0)); // -> 10.0000
Debug.WriteLine(calc_significant_figures(10.0000500M, 6, 0)); // -> 10.0001
Debug.WriteLine(calc_significant_figures(1050000.00M, 2, 1)); // -> 1000000
Debug.WriteLine(calc_significant_figures(10.0000500M, 6, 1)); // -> 10.0000
Debug.WriteLine(calc_significant_figures(1040000.00M, 2, 2)); // -> 1100000
Debug.WriteLine(calc_significant_figures(10.0000400M, 6, 2)); // -> 10.0001
}
static decimal calc_significant_figures(decimal value, int digits, int type)
{
if (value == 0M)
{
return 0M;
}
var integer_part = (int)Math.Log10(Math.Abs((int)value)) + 1;
//var decimal_part = decimal.GetBits(value)[3] >> 16 & 0x0FF;
switch (type)
{
case 0:
{
var scale = (decimal)Math.Pow(10, integer_part);
var result = scale * Math.Round(value / scale, digits, MidpointRounding.AwayFromZero);
//return Math.Round(result, decimal_part);
var decimals = digits - integer_part;
return Math.Round(result, (decimals < 0) ? 0 : decimals);
}
case 1:
{
var scale = (decimal)Math.Pow(10, integer_part - digits);
return scale * Math.Floor(value / scale);
}
case 2:
{
var scale = (decimal)Math.Pow(10, integer_part - digits);
return scale * Math.Ceiling(value / scale);
}
}
throw new Exception("error: calc_significant_figures");
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme