Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

math - C# decimal take ceiling 2

I want to round my decimal value like 2.2222 to 2.23. When I use round,

decimal a = Math.Round((decimal)2.222, 2);

When I use ceiling, it cause 3

decimal c = Math.Ceiling((decimal)2.22);

How can I get 2.2222 to 2.23 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
public static decimal CeilingAfterPoint(this decimal number, int digitsAfterPoint) {
    return Math.Ceiling(number * (decimal)Math.Pow(10, digitsAfterPoint))
           / (decimal)Math.Pow(10, digitsAfterPoint);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...