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?
Assuming that ‘common syntax’ means that you need one solution to give both outputs,
String.Format("${0:#.##}", x)does the trick. Whenxis1.00M, the result will be"$1". whenxis1.22M, the result is"$1.22".