itmanir
itmanir

Reputation: 177

Round a float number in c#

i get 4 byte data in network as float point number and when i convert it to a float number, result is like this: "7.346952E-40", but i want to show this number to user. when i round this number with Math.Round() function, result is "0.000". how can i round this float number to a simple float number like this: 123.456?

Upvotes: 0

Views: 139

Answers (3)

Scott Chamberlain
Scott Chamberlain

Reputation: 127563

The number 7.346952E-40 is actually 0.0000000000000000000000000000000000000007346952, so when you round it to 4 decimal places you get 0.0000.

Upvotes: 3

AFract
AFract

Reputation: 9680

7.346952E-40 (7.346952 x 10^-40) is actually really close of 0, your rounding function is correct.

You can also use number.ToString("0.000") or number.ToString("#.###") to display your number depending of if you want only significant digits or a constant length number, like for currencies.

Upvotes: 0

J. Yan
J. Yan

Reputation: 1

Just Console.writeline(xxx.ToString("0.000"))

Upvotes: -1

Related Questions