C
C#3y ago
?

Hexadecimal format specifier not working

for(int X = 1 ; X <= 5 ; X++)
{
var F = Math.Pow(63, X);
Console.WriteLine($"{F:X}");
}
for(int X = 1 ; X <= 5 ; X++)
{
var F = Math.Pow(63, X);
Console.WriteLine($"{F:X}");
}
For some reason this is not working and I receive an error: FormatException: Format specifier was invalid.
4 Replies
?
?OP3y ago
And even if I try F.ToString("X") it's also not working
mtreit
mtreit3y ago
It's because your variable F is a floating point number. It needs to be an integer. Also don't use capital letters for variable names.
MODiX
MODiX3y ago
mtreit#6470
REPL Result: Success
for (int x = 1; x <= 5; x++)
{
var f = (int)Math.Pow(63, x);
Console.WriteLine($"0x{f:X}");
}
for (int x = 1; x <= 5; x++)
{
var f = (int)Math.Pow(63, x);
Console.WriteLine($"0x{f:X}");
}
Console Output
0x3F
0xF81
0x3D0BF
0xF05F01
0x3B27613F
0x3F
0xF81
0x3D0BF
0xF05F01
0x3B27613F
Compile: 648.175ms | Execution: 85.741ms | React with ❌ to remove this embed.
?
?OP3y ago
Okay, that worked! I didn't know it works only for integers, thanks!
Want results from more Discord servers?
Add your server