开发者

how to truncate an double datatype value variable

开发者 https://www.devze.com 2022-12-30 22:56 出处:网络
i havean class public class Score { public doublemarks { get; set; } } now in my code List<Level1> Obj= new List<Level1>();

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

精彩评论

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