0

I use following code to convert input to comma separated string in INR:

decimal input = 1111111111.59m;
string result = input.ToString("C", new CultureInfo("EN-in"));

I want to remove the trailing 0s now, how do i do this?

for example:

decimal input = 1111111111.00m;
Output should be 1111111111
Sahil Sharma
  • 3,847
  • 6
  • 48
  • 98

4 Answers4

2

string result = input.ToString("c0", new CultureInfo("EN-in"));

Update:

So you want output "123.45" for input 123.45 and output "123" for input 123.00. You can't achieve these 2 different formats without conditional operator, String.Format() will produce only one output format for you.

The code is simple though:

string format = Decimal.Round(input) == input ? "c0" : "c";
string output = input.ToString(format);
CodeFuller
  • 30,317
  • 3
  • 63
  • 79
0

string output = input.ToString("0");

Eugene
  • 365
  • 6
  • 21
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – kayess Apr 07 '17 at 09:12
0

Following code should work :

string results = input.ToString("0.##");
Biswabid
  • 1,378
  • 11
  • 26
0

The simplest thing to convert is convert into int.

int d = convert.toInt32(1111.00);

or use any math function as suggested.

How to remove decimal part from a number in C#

How do I format a C# decimal to remove extra following 0's?

Edit As I understand just try

Console.WriteLine(d.ToString("0.#####"));

Seet this url :- Best way to display decimal without trailing zeroes

Community
  • 1
  • 1
Ajay2707
  • 5,690
  • 6
  • 40
  • 58