1

I have a decimal like like "-00.20300"

I want output like this

"-.203"

Any easy way to achieve this without removing the negative sign remove 0s and then reattaching negative sign?

Lightsout
  • 3,454
  • 2
  • 36
  • 65
  • 4
    Parse it to an *actual* decimal and use the number format you desire. – Ňɏssa Pøngjǣrdenlarp Nov 11 '21 at 00:20
  • *without removing the negative sign remove 0s and then reattaching negative sign?* - why impose this restriction? – Caius Jard Nov 11 '21 at 01:06
  • Just trying to see if there is more elegant way since I usually take the brute force approach – Lightsout Nov 11 '21 at 01:41
  • You can convince a decimal type to forget how many digits of precision it has with `value / 1.000000000000000000000000000000000m;` – Jeremy Lakeman Nov 11 '21 at 01:41
  • Will the input string always contain a '.' or could "100" also be a possible input? What would you expect if the input is "0.0" - shall both zeroes be removed? – Klaus Gütter Nov 11 '21 at 03:47
  • First you asked for "easy" (but shunned an actual "easy"), then you asked for "more elegant", which is a problem because it isn't really objective. I think you need to better define the parameters we need to work to, and the data we will work on.. – Caius Jard Nov 11 '21 at 05:59

3 Answers3

0

Assuming you know the number of decimal digits you want in your output:

decimal val = -00.20300M;
string result = val.ToString(".###", System.Globalization.CultureInfo.InvariantCulture);
// result is "-.203"

If you don't know the number of decimal spots, you can use this method to calculate it (and include the TrimEnd because you're ignoring trailing zeros), then make the format from the count.

decimal val = -00.20300M;
var decimalCount = val.ToString(System.Globalization.CultureInfo.InvariantCulture)
    .TrimEnd('0')
    .SkipWhile(c => c != '.')
    .Skip(1)
    .Count();

var format = "." + new string('#', decimalCount);
string result = val.ToString(format, System.Globalization.CultureInfo.InvariantCulture);
// result is "-.203"
gunr2171
  • 16,104
  • 25
  • 61
  • 88
0

Nyssa's comment is on the right track, and would be the right way to do things, but unfortunately none of the string formats built in to C# provide the behavior that you want.

Gunr's answer is correct, but only works when you know the number of useful digits.

Here is a general solution:

public static string TrimmedStringConversion(string numberString)
{
    const string minusSign = "-";
    var returnString = string.Empty;
    if (numberString.StartsWith(minusSign))
    {
        returnString = minusSign;
        numberString = numberString.Remove(0,1);
    }

    returnString += numberString.Trim('0');
    return returnString;
}

And the unit test/usage:

[TestMethod]
public void TestStringConversion()
{
    var startingString = "-00.20300";
    var convertedString = TrimmedStringConversion(startingString);
    Assert.AreEqual("-.203", convertedString);
}
Nigel
  • 2,961
  • 1
  • 14
  • 32
0

you have to format the decimal value

decimal decValue = -00.20300M;
string result = decValue.ToString(".###");

Console.WriteLine(result);
Modulus
  • 13
  • 4