1

I have a decimal in in my program which could possibly have trailing zeros after a decimal point and I would like to get rid of them.

I wish to do this by converting the decimal to a string and only using a sub-string based on where a decimal point is found and if the content after the decimal point is just zeros. This is what I came up with:

string number = Convert.ToString(MyDouble);
if (number.Contains("."))//If there are trailing zeros
{
    //If the numbers after the decimal point (when taken as an integer) equal 0
    if(Convert.ToInt16(number.Substring(number.IndexOf("."))) == 0)
    {
        //Remove anything after the decimal point
        number = number.Substring(0, number.Length - number.IndexOf("."));
    }
}
result = Convert.ToDecimal(number);

But this doesn't work. What's wrong with it?

Toby Smith
  • 1,505
  • 2
  • 15
  • 24
  • you can do this simply using a custom format specifier (in particular, look at '#' vs. '0') https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx – hatchet - done with SOverflow Aug 25 '15 at 17:50
  • 1
    I don't really understand what this is trying to accomplish. Generally one would store numeric values in memory simply as their numeric (not string) representation. And then simply apply a string formatter when displaying them in a UI of some kind. Why this conversation? – David Aug 25 '15 at 17:50
  • Can you remove all unrelated code from your sample and replace values with constants: `number` is primary candidate? – Alexei Levenkov Aug 25 '15 at 17:51
  • @David I assume it is exercise in string manipulation. Clearly rounding/trimming should be done directly on numbers (there are plenty of existing question on that)... Or maybe it is to show locale-specific issues with numbers conversion to string... – Alexei Levenkov Aug 25 '15 at 17:52
  • If linked duplicate does not cover all cases - there are more details in if you need particular number of digits after decimal point : http://stackoverflow.com/questions/164926/c-sharp-how-do-i-round-a-decimal-value-to-2-decimal-places-for-output-on-a-pa – Alexei Levenkov Aug 25 '15 at 18:06
  • I don't believe my question is a duplicate. I was asking why my code didn't work, not asking for other solutions. "But this doesn't work. What's wrong with it?" – Toby Smith Aug 25 '15 at 18:08
  • @user3573985 "does not work" is not explanation of the problem (it is actually surprising that post did not got about -2 votes due to it). You decided *not to update your question* based on comments asking for clarification - so in general it means that OP don't really need help with the code as posted (whether it applies to your particular case it is up to you). Also note that you've found that question as posted has almost nothing to do with your goal... – Alexei Levenkov Aug 25 '15 at 18:20

3 Answers3

2

Linq makes this pretty easy (if you don't want to convert the value):

using System;
using System.Linq;
                    
public class Program
{
  public static void Main()
  {
    var a = "10.001000";
    var b = a.Reverse().SkipWhile(i => i == '0').Reverse();
    var c = new String(b.ToArray());
        
    Console.WriteLine(a);
    Console.WriteLine(c);
  }
}

Results:

10.001000

10.001

Community
  • 1
  • 1
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
1

ToString() can do the trick. here is how to do so -

     decimal a = 122.00000m;
     string trimmed = a.ToString("#.00");

and if you want get rid of all the zeros just do it like this-

    decimal a = 122.00000m;
     string trimmed = a.ToString("#");

So you can define a lot of format codes in your applicaiton as constants, and use them as you need... no logic needed.

Kapoor
  • 1,388
  • 11
  • 21
0

I have found a solution. If the decimal (when converted to a string) has a decimal point in it a while loop keeps removing the last character all the time the last character is a 0.

string number = Convert.ToString(MyDouble);
if (number.Contains("."))
{
    while(number.Substring(number.Length - 1) == "0")
    {
        number = number.Substring(0, number.Length - 1);
    }
}
result = Convert.ToDecimal(number);
Toby Smith
  • 1,505
  • 2
  • 15
  • 24
  • Really bad idea for what question is asking (whether it works for you or not) - `% 1` is much better at extracting part after decimal point. Also code seem to be doing completely different thing from one in the post - orignal code tries to remove zeros IF there is nothing but zeros, this one - removes zeros up to first non-zero digit. You may need to edit question to align both. – Alexei Levenkov Aug 25 '15 at 18:01
  • @AlexeiLevenkov you're right. But I did mis-phrase my question. I would like `2.0200` to become `2.02` as well as `3.00` becoming `3`. – Toby Smith Aug 25 '15 at 18:04