vb.netdecimalexp

How to write in vb.net a decimal number without rounding the 12 digits after the decimal point?


I try to write the result of the division of the number 5991 by 2987 in vb.net, without rounding the decimal or floating point, strictly equal to 2.005691329092 on 12 digits after the decimal point. The decimal part (0.05691329092) is also noted 17/2987 in scientific notation. My tests always led me to 2.005691329093 rounding!

2 17/2987

Solution

  • Actual value is correct, issue is how to format value to display it as expected.
    There are no built-in method, but you can multiply value by required amount of digits after decimal point and truncate it.

    Dim value As Double = 5991.0 / 2987.0
    value = Math.Truncate(value * Math.Pow(10, 12)) / Math.Pow(10, 12)
    
    Console.WriteLine($"Result: {value:F12}")