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. When x
is 1.00M
, the result will be "$1"
. when x
is 1.22M
, the result is "$1.22"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With