Console.Write - rounds.!!!

  • Thread starter Thread starter Piotrekk
  • Start date Start date
P

Piotrekk

Hi
I have a numerical problem.
Iam calculating vector is that is 0.xxxxxxxxxxx
and the calculating error for this vector. After that i correct first
vector.
The vectors differ on ~8 place after floating point. Debugger sees the
difference, but when i use Write method, i cant, because its rounding
my vectors.
Anyone can help?
PK
 
Instead of using Console.Write(float), which gives you the default format,
you can use Console.Write(float.ToString()), which allows you to specify your
own format for the output. For example:

float fValue = ...;
Console.Write(fValue.ToString("R"))

This would output all digits of the floating point number with no rounding.

Hope this helps.

Brian.
 
Brian C. Barnes said:
Instead of using Console.Write(float), which gives you the default format,
you can use Console.Write(float.ToString()), which allows you to specify your
own format for the output. For example:

float fValue = ...;
Console.Write(fValue.ToString("R"))

This would output all digits of the floating point number with no rounding.

No, that doesn't output all the digits. It outputs enough to be able to
get back to that exact floating point number when parsing, which isn't
the same thing.

If you want to get the *exact* value of a floating point number, see
http://www.pobox.com/~skeet/csharp/DoubleConverter.cs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top