Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format chokes on decimal values?

I expect both tests below (written for NUnit) to pass, but the Decimal version fails with, "System.FormatException: Format specifier was invalid", as does a Double version. I cannot figure out why. Can someone please shed light?

Thanks; Duncan

[Test]
public void Integer_Format_Hex()
{
    //Assume
    Int32 myValue = 11101110;

    //Arrange

    //Act

    //Assert
    Assert.That( String.Format( "0x{0:X8}" , myValue ) , Is.EqualTo( "0x00A963B6" ) );
}

[Test]
public void Decimal_Format_Hex()
{
    //Assume
    Decimal myValue = 11101110m;

    //Arrange

    //Act

    //Assert
    Assert.That( String.Format( "0x{0:X8}" , myValue ) , Is.EqualTo( "0x00A963B6" ) );
}

[Test]
public void Decimal_Format_Hex2()
{
    //Assume
    Decimal myValue = 11101110m;

    //Arrange

    //Act

    //Assert
    Assert.That( myValue.ToString( "X" ) , Is.EqualTo( "00A963B6" ) );
}
like image 765
Duncan Avatar asked Feb 26 '23 21:02

Duncan


1 Answers

Exerpt from http://msdn.microsoft.com/en-us/library/fzeeb5cd(v=VS.90).aspx

"The format parameter can be any valid standard numeric format specifier except for D, R, and X...."

Awesome.

like image 156
Duncan Avatar answered Mar 03 '23 14:03

Duncan