.NET 11 Preview - Round-tripping Hex-Formatted Fractional Numbers
[C#, .NET, .NET 11 Preview]
Yesterday’s post, “.NET 11 Preview - Formatting Floating Point Numbers In Hex”, looked at how to format fractional numbers in hex, which is now possible in .NET 11.
var natruralLog = Math.E;
Console.WriteLine(natruralLog.ToString("x"));
In this post, we will look at the reverse - retrieving the numerical fractional value from its hex representation.
First, we obtain the hexValue as a string:
var hexValue = natruralLog.ToString("x");
Next, we parse the value back to a number:
var number = double.Parse(hexValue, NumberStyles.HexFloat);
Here we are using the double.Parse method in conjunction with the NumberStyles.HexFloat enum in the System.Globaization namespace, which instructs how to parse the string.
We can then verify that our original number is equal to the parsed number.
// Print number
Console.WriteLine(number);
// Verify parity
Console.WriteLine(number == natruralLog);
This will display the following:

We can see here that our value was successfully round-tripped.
TLDR
Fractional numbers formatted as hex can be round-tripped successfully with preserved accuracy.
The code is in my GitHub.
Happy hacking!