0

This may be something similar to other but my understanding is not great so I'm trying to see if anyone can help me understand. I want to change the decimal place of a number, for example if the number is 0.5 I want it to convert it to .50, likewise if it was 0.25 then .25, etc, I'm guessing this is so simple but I can't seem to understand how to do this? My unit test code I have is below and the first one passes but not the others

Updated Unit Test

[TestFixture]
public class ProbabilityDisplayConverterTests 
{
    public ProbabilityDisplayConverter underTest = new ProbabilityDisplayConverter();

    [Test]
    public void Convert_ConvertsWholeDecimal()
    {
        var value = (string)underTest.Convert(1, typeof(decimal), null, CultureInfo.CurrentCulture);
        Assert.AreEqual("1", value);
    }

    [Test]
    public void Convert_ConvertsHalfToDecimal()
    {
        var value = (string)underTest.Convert(0.5, typeof(decimal), null, CultureInfo.CurrentCulture);
        Assert.AreEqual(".50", value);
    }

    [Test]
    public void Convert_ConvertsDecimal()
    {
        var value = (string)underTest.Convert(0.25, typeof(decimal), null, CultureInfo.CurrentCulture);
        Assert.AreEqual(".25", value);
    }

    [Test]
    public void Convert_ConvertsWholeNumberDecimal()
    {
        var value = (string)underTest.Convert(0.3, typeof(decimal), null, CultureInfo.CurrentCulture);
        Assert.AreEqual(".30", value);
    }
}

EDIT

    public class ProbabilityDisplayConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value = value.ToString("G", CultureInfo.InvariantCulture);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Wesley Lomax
  • 2,067
  • 2
  • 20
  • 34
Ryan Archibald
  • 215
  • 2
  • 18

2 Answers2

0

Try this

number = .0023;
Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));

reference: https://msdn.microsoft.com/ru-ru/library/dwhawy9k(v=vs.110).aspx

nomail
  • 635
  • 6
  • 21
0

I´m not sure if I miss something in this question, but why not simply use the format option of the ToString method?

        (0.5m).ToString(".00");
        (0.15m).ToString(".00");

does exactly what the OP Need (of course you get your local decimal point char)

Thomas Krojer
  • 1,018
  • 7
  • 9
  • I get an error saying "no overload for method 'ToString' takes 1 argument" :( – Ryan Archibald Sep 24 '15 at 14:54
  • This is because your first parameter is an "object", first it needs unboxing with a cast to the appropriate type. – PaulF Sep 24 '15 at 14:58
  • This might be useful for you to modify your code : http://stackoverflow.com/questions/11246820/how-to-cast-an-object-to-type-passed-to-a-function. If you want to use Convert.ChangeType - you will have to change the name of your method from Convert. – PaulF Sep 24 '15 at 15:11
  • Thanks that helped a lot, my final question is how can I handle the number 1, as if the number is 1.0 I want it to display 1 only? – Ryan Archibald Sep 24 '15 at 15:54