Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take away fraction part from decimal?

How to take away fraction part while formatting decimal type in .NET? I need common syntax for both variants. Is there any gentle solution?

            decimal a = 1.22M;
            decimal b = 1.00M;

            String.Format("${0}", a); // result is $1.22
            String.Format("${0}", b); // result is $1.00, should be $1, HOW?
like image 792
Roman Pushkin Avatar asked Dec 07 '22 01:12

Roman Pushkin


1 Answers

Assuming that 'common syntax' means that you need one solution to give both outputs, String.Format("${0:#.##}", x) does the trick. When x is 1.00M, the result will be "$1". when x is 1.22M, the result is "$1.22".

like image 116
Joren Avatar answered Feb 14 '23 12:02

Joren