#I dont really get this.. (Weighting Machine, C#)

5 messages · Page 1 of 1 (latest)

sour yarrow
#
using System;

class WeighingMachine
{
    private int precision;
    private double weight;
    private double tare=5;
    private double weight2;
    private string weight3;


    public WeighingMachine(int precision = 3)
    {
        Precision = precision;
    }
   
    public int Precision
    {
    get { return precision; }
        set
        {
            if (value <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(value), "Precision must be a positive integer.");
            }
            precision = value;
        }
    }

    public double Weight
    {
        get { return weight; }
        set {
            if(value<0)
            {
                throw new ArgumentOutOfRangeException();
            }
            else{ weight = value; }
        }
    }

    public string DisplayWeight
    {

         get { weight2 = weight-tare; weight3 = weight2.ToString($"F{precision}"); return $"{weight3} kg";}
    }

    public double TareAdjustment
    {
        get { return tare; }
        set { tare = value; }
    }
}
#

I know the DisplayWeight getter is stupid, but i tried several ways with Math.Round and it just didnt want to add the extra 0

molten oracle
#

Rounding is a bit iffy in Programming, but these Functions should do it correctly.