开发者

how to truncate an double datatype value variable

i have an class

public class Score
{
    public double  marks { get; set; }
}

now in my code

List<Level1> Obj= new List<Level1>();
 Score Object= new Score();
 l1.m开发者_JS百科arks=94.345454;
Obj.Add(Object)

// now whiling adding the object to list i need to trunacte the value to 94.34 and store in the list obj. so that i need to get output has 94.34 how can i do it. thanks in advance

prince


Keep the value as a double, and only format it on output.

string output = doubleValue.ToString("F2");

Source. As an example:

doubleNumber = -1898300.1987;
Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture));  
// Displays -1898300.2

Console.WriteLine(doubleNumber.ToString("F3", 
                  CultureInfo.CreateSpecificCulture("es-ES")));
// Displays -1898300,199  

Notice the decimal comma on the last output as the culture is set to Spanish.


You could do this in the marks property setter:

value -= value % 0.01

this will subtract 0.005454 from value and so leave you with the correct number of decimal points (in this case, 2)

something like:

public class Score {
    private double m_marks;

    public double marks {
        get { return m_marks; }
        set { m_marks = value - (value % 0.01); }
    }
}


You could use Math.Round(number, precision) as shown on msdn

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜