tags:

views:

182

answers:

3

Hi, this is my issue:

I made a query to SQL server to get some data via a Stored Procedure, the returned value was this:

10219150

Then, on an assembly (I don't have the source code of that assembly, I reflected the file to view the code) someone made this:

Ammount = Convert.ToSingle(10219150); //the value from the stored procedure

So, when I invoke that method which makes some stuff and does the final conversion, it returns this value:

1.021315E+7

How is that possible?? why does the Convert.ToSingle add extra decimal positions?? I don't understand.

How could I fix it, is there a way that i can reverse that conversion on my code when I invoke that method of the assembly? (cause I can't rewrite that assembly file, it's too big and as i mentioned earlier, i don't have the source code to fix the conversion); so can can get

From this: 1.021315E+7 To this: 10219150 again (restore the correct value without that conversion)

Hope I made myself clear.

Thanks in advance.

+1  A: 

The conversion to single isn't adding extra precision.

10219150 is 1.021315E+7 (which is just another way of writing 1.021315 * 107).

The method you are using to print out the value is just using scientific notation to display the number.

If you are printing the number then you need to set the formatting options.

    float amount = Convert.ToSingle("10219150");
    string toPrint = string.Format("{0:N}", amount);

Will print the number as:

"10,219,150.00"

To get no decimal places use "{0:N0}" as the format string.

ChrisF
This is how i'm printing the value: String.Format("{0:0,0}", Convert.ToDouble(1.021315E+7)) and on the front it looks like: 10.219.150.000.000 ?? i never used Convert.ToSingle before, so that's why I don't understand that behavior.
lidermin
Thanks a lot! you made my day.
lidermin
A: 

You have two issues. One is easily solved, and the other may be more difficult or impossible.

As ChrisF stated, 1.021315E+7 is simply another way of writing 10219150. (The E+7 part in Scientific Notation means to shift the decimal point 7 places to the right.) When you format your single precision value, you can use

fvalue.ToString("f0");

to display as an integer, rather than in Scientific Notation.

The bigger problem, unfortunately, is that a single precision float can only hold 7 significant digits, and in your example you are storing 8. Therefore, the last digit may be rounded. (Since it happens to be 0 in your case, the rounding might not have been noticed.)

If that loss of precision is critical, you would likely need to fetch the value from the database as a long, or as a double-precision value (depending on the type of data returned.) Each of these types can hold more significant digits.

Eric Pi
A: 

When the value is converted to Single, it's rounded as it contains more significant digits that can fit in a Single. If you convert 10213153 to Single you also end up with 1.021315E+7 i.e. 10213150.

As the code uses a Single to store the amount, there is nothing that you can do to make it handle the current value correctly. The amount simply can not be represented correctly as a Single.

You either have to use lower values, or change the code.

Guffa