String.Format soffoca su valori decimali?
-
29-09-2019 - |
Domanda
mi aspetto entrambi i test al di sotto (scritto per NUnit) per passare, ma la versione decimale viene a mancare con "System.FormatException: di formato non era valido", così come una versione doppia. Non riesco a capire perché. Qualcuno può fare luce?
Grazie; 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" ) );
}
Soluzione
http://msdn.microsoft .com / it-it / library / fzeeb5cd (v = VS.90) aspx
"Il parametro di formato può essere qualsiasi identificatore di formato numerica standard valido ad eccezione di D, R e X ...."
Impressionante.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow