0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

n桁の有効数字に丸める

0
Last updated at Posted at 2020-07-02
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");
}

有効数字 @wikipedia

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?