Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format a decimal without separators and without decimals?

Tags:

c#

How can I format a decimal to be converted to a string without group separators and without decimals?

For eg: "1,234.56" should be displayed as "1234".

like image 533
Andrew Avatar asked Mar 07 '13 04:03

Andrew


1 Answers

This almost works, but rounds up:

Decimal d = 1234.56M;
string s = string.Format("{0:0}", d);
Console.WriteLine(s);

Outputs: 1235

As @Jon Skeet suggested, you could cast to an integer type (assuming it was large enough to hold your largest decimal value):

Decimal d = 1234.56M;
string s = string.Format("{0}", (long)d);
Console.WriteLine(s);

Outputs: 1234

Demo: http://ideone.com/U4dcZD

like image 81
Mitch Wheat Avatar answered Sep 18 '22 23:09

Mitch Wheat