Showing posts with label float. Show all posts
Showing posts with label float. Show all posts

Tuesday, February 14, 2012

Converting SQLMoney to c# float ?

I'm using c# 2.0.

I have a datareader that reads an invoice line item from a table in my SQL Server database. The UnitPrice field is a Money field in SQL server. At the same time I have an invoice class in my application that has a UnitPrice property which is a float.

How do I convert the SQLMoney to a float using my datareader?

Right now I have:

cm.CommandText =

"SELECT ItemID, Quantity, UnitPrice, Discount FROM tblInvoiceLineItems";using (SqlDataReader dr = cm.ExecuteReader())

{

while (dr.Read())

{

LineItem li =newLineItem();

li.UnitPrice = (

float)(double)dr.GetFloat(3); // cast error here.

lineItems.Add(li);

}

}

It will be easier to use DECIMAL in the database so your employer will not loose money and believe me that is a high probability with FLOAT. This makes it easy to use Visual Basic financial class to calculate your price in the application then use standard convert to convert your value to Decimal which is the same as money in SQL Server. Why because you can only set precision and scale with DECIMAL and NUMERIC in SQL Server. Just a thought the link below covers the FCL(framework class library) types, ADO.NET types and SQL Server types. Hope this helps.

http://msdn2.microsoft.com/en-us/library/ms131092.aspx

Sunday, February 12, 2012

Converting Reals into Floats

Are there any conversion issues I should know when
converting a real into a float ?
Thanks
J
Hi
A real and a float are the same data type, just different names. Both are
approximate and can not represent the exact value.
Regards
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Blond Moment" <anonymous@.discussions.microsoft.com> wrote in message
news:14c101c50452$95db8c30$a601280a@.phx.gbl...
> Are there any conversion issues I should know when
> converting a real into a float ?
> Thanks
> J
|||Thanks Mike, your a sweetie.

>--Original Message--
>Hi
>A real and a float are the same data type, just different
names. Both are
>approximate and can not represent the exact value.
>Regards
>--
>Mike Epprecht, Microsoft SQL Server MVP
>Zurich, Switzerland
>IM: mike@.epprecht.net
>MVP Program: http://www.microsoft.com/mvp
>Blog: http://www.msmvps.com/epprecht/
>"Blond Moment" <anonymous@.discussions.microsoft.com>
wrote in message
>news:14c101c50452$95db8c30$a601280a@.phx.gbl...
>
>.
>
|||To add to Mike's response, note that you may end up with different values
when converting between float and real data types when using the a float
precistion of 25 or greater. The default float precistion is 53:
DECLARE @.MyReal real
DECLARE @.MyFloat float
SET @.MyReal = 1.06
SET @.MyFloat = 1.06
SELECT @.MyReal AS MyReal, @.MyFloat AS MyFloat
SET @.MyFloat = @.MyReal
SELECT @.MyReal AS MyReal, @.MyFloat AS MyFloat
SET @.MyFloat = 1.06
SET @.MyReal = @.MyFloat
SELECT @.MyReal AS MyReal, @.MyFloat AS MyFloat
Hope this helps.
Dan Guzman
SQL Server MVP
"Blond Moment" <anonymous@.discussions.microsoft.com> wrote in message
news:14c101c50452$95db8c30$a601280a@.phx.gbl...
> Are there any conversion issues I should know when
> converting a real into a float ?
> Thanks
> J
|||Mike,
Real and float are not the same data type, though they are in
the same data type family. The capabilities of the real and
float types in SQL Server derive from the internal format
used for their representation: real stores floating point values
according to the 4-byte IEEE standard, and float uses the
8-byte IEEE format.
Both real and float represent values exactly, but the reason they
are called "approximate" data types is that the exact values they
store do not match the decimal fixed-point values we use in the
familiar base-10 number system. Real and float store values exactly
in base-2 representation, so for example both real and float can store
11.5625 (or 11 9/16) exactly, but neither can store the value 0.1
(or 1/10) exactly, and an assignment of 1/10 to a real or float will
result in a stored value near to, but not equal to, 1/10.
Floats can store a wider range and precision than reals, but
the IEEE 4-byte and 8-byte formats are designed in such a
way that every representable real can be represented exactly
as a float.
All that said, the bottom line is that if a real value is assigned
to a float, no loss of precision will occur. It is similar to assigning
a decimal(5,2) value to a decimal(10,4).
SQL Server's use of "exact" and "approximate" to describe
decimal and float/real types is misleading, in my opinion. Both
type families store exact values. The values a decimal type like
decimal(3,1) can store are evenly spaced base-10 fixed-precision
numbers (-99.9, -99.8, ..., 99.8, 99.9), wherease the values a
float can store are unevenly-spaced base-2 floating-precision
numbers of the form (mantissa) * 2^(exponent), for a set of
evenly-spaced (mantissa) values >= 1/2 and < 1 and all values
of (exponent) between -1023 and +1023 (omitting some details
about the representation of infinity, negative zero, and reduced-
precision "denormalized" values with absolute value less
than 2^(-1023)).
-- Steve Kass
-- Drew University
-- Ref: E46D858E-5EFC-44C0-9EEB-F97C291E1B0D
Mike Epprecht (SQL MVP) wrote:

>Hi
>A real and a float are the same data type, just different names. Both are
>approximate and can not represent the exact value.
>Regards
>--
>Mike Epprecht, Microsoft SQL Server MVP
>Zurich, Switzerland
>IM: mike@.epprecht.net
>MVP Program: http://www.microsoft.com/mvp
>Blog: http://www.msmvps.com/epprecht/
>"Blond Moment" <anonymous@.discussions.microsoft.com> wrote in message
>news:14c101c50452$95db8c30$a601280a@.phx.gbl...
>
>
>

Converting Reals into Floats

Are there any conversion issues I should know when
converting a real into a float ?
Thanks
JHi
A real and a float are the same data type, just different names. Both are
approximate and can not represent the exact value.
Regards
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Blond Moment" <anonymous@.discussions.microsoft.com> wrote in message
news:14c101c50452$95db8c30$a601280a@.phx.gbl...
> Are there any conversion issues I should know when
> converting a real into a float ?
> Thanks
> J|||Thanks Mike, your a sweetie.
>--Original Message--
>Hi
>A real and a float are the same data type, just different
names. Both are
>approximate and can not represent the exact value.
>Regards
>--
>Mike Epprecht, Microsoft SQL Server MVP
>Zurich, Switzerland
>IM: mike@.epprecht.net
>MVP Program: http://www.microsoft.com/mvp
>Blog: http://www.msmvps.com/epprecht/
>"Blond Moment" <anonymous@.discussions.microsoft.com>
wrote in message
>news:14c101c50452$95db8c30$a601280a@.phx.gbl...
>> Are there any conversion issues I should know when
>> converting a real into a float ?
>> Thanks
>> J
>
>.
>|||To add to Mike's response, note that you may end up with different values
when converting between float and real data types when using the a float
precistion of 25 or greater. The default float precistion is 53:
DECLARE @.MyReal real
DECLARE @.MyFloat float
SET @.MyReal = 1.06
SET @.MyFloat = 1.06
SELECT @.MyReal AS MyReal, @.MyFloat AS MyFloat
SET @.MyFloat = @.MyReal
SELECT @.MyReal AS MyReal, @.MyFloat AS MyFloat
SET @.MyFloat = 1.06
SET @.MyReal = @.MyFloat
SELECT @.MyReal AS MyReal, @.MyFloat AS MyFloat
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Blond Moment" <anonymous@.discussions.microsoft.com> wrote in message
news:14c101c50452$95db8c30$a601280a@.phx.gbl...
> Are there any conversion issues I should know when
> converting a real into a float ?
> Thanks
> J|||Mike,
Real and float are not the same data type, though they are in
the same data type family. The capabilities of the real and
float types in SQL Server derive from the internal format
used for their representation: real stores floating point values
according to the 4-byte IEEE standard, and float uses the
8-byte IEEE format.
Both real and float represent values exactly, but the reason they
are called "approximate" data types is that the exact values they
store do not match the decimal fixed-point values we use in the
familiar base-10 number system. Real and float store values exactly
in base-2 representation, so for example both real and float can store
11.5625 (or 11 9/16) exactly, but neither can store the value 0.1
(or 1/10) exactly, and an assignment of 1/10 to a real or float will
result in a stored value near to, but not equal to, 1/10.
Floats can store a wider range and precision than reals, but
the IEEE 4-byte and 8-byte formats are designed in such a
way that every representable real can be represented exactly
as a float.
All that said, the bottom line is that if a real value is assigned
to a float, no loss of precision will occur. It is similar to assigning
a decimal(5,2) value to a decimal(10,4).
SQL Server's use of "exact" and "approximate" to describe
decimal and float/real types is misleading, in my opinion. Both
type families store exact values. The values a decimal type like
decimal(3,1) can store are evenly spaced base-10 fixed-precision
numbers (-99.9, -99.8, ..., 99.8, 99.9), wherease the values a
float can store are unevenly-spaced base-2 floating-precision
numbers of the form (mantissa) * 2^(exponent), for a set of
evenly-spaced (mantissa) values >= 1/2 and < 1 and all values
of (exponent) between -1023 and +1023 (omitting some details
about the representation of infinity, negative zero, and reduced-
precision "denormalized" values with absolute value less
than 2^(-1023)).
-- Steve Kass
-- Drew University
-- Ref: E46D858E-5EFC-44C0-9EEB-F97C291E1B0D
Mike Epprecht (SQL MVP) wrote:
>Hi
>A real and a float are the same data type, just different names. Both are
>approximate and can not represent the exact value.
>Regards
>--
>Mike Epprecht, Microsoft SQL Server MVP
>Zurich, Switzerland
>IM: mike@.epprecht.net
>MVP Program: http://www.microsoft.com/mvp
>Blog: http://www.msmvps.com/epprecht/
>"Blond Moment" <anonymous@.discussions.microsoft.com> wrote in message
>news:14c101c50452$95db8c30$a601280a@.phx.gbl...
>
>>Are there any conversion issues I should know when
>>converting a real into a float ?
>>Thanks
>>J
>>
>
>

Converting Reals into Floats

Are there any conversion issues I should know when
converting a real into a float ?
Thanks
JHi
A real and a float are the same data type, just different names. Both are
approximate and can not represent the exact value.
Regards
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Blond Moment" <anonymous@.discussions.microsoft.com> wrote in message
news:14c101c50452$95db8c30$a601280a@.phx.gbl...
> Are there any conversion issues I should know when
> converting a real into a float ?
> Thanks
> J|||Thanks Mike, your a sweetie.

>--Original Message--
>Hi
>A real and a float are the same data type, just different
names. Both are
>approximate and can not represent the exact value.
>Regards
>--
>Mike Epprecht, Microsoft SQL Server MVP
>Zurich, Switzerland
>IM: mike@.epprecht.net
>MVP Program: http://www.microsoft.com/mvp
>Blog: http://www.msmvps.com/epprecht/
>"Blond Moment" <anonymous@.discussions.microsoft.com>
wrote in message
>news:14c101c50452$95db8c30$a601280a@.phx.gbl...
>
>.
>|||To add to Mike's response, note that you may end up with different values
when converting between float and real data types when using the a float
precistion of 25 or greater. The default float precistion is 53:
DECLARE @.MyReal real
DECLARE @.MyFloat float
SET @.MyReal = 1.06
SET @.MyFloat = 1.06
SELECT @.MyReal AS MyReal, @.MyFloat AS MyFloat
SET @.MyFloat = @.MyReal
SELECT @.MyReal AS MyReal, @.MyFloat AS MyFloat
SET @.MyFloat = 1.06
SET @.MyReal = @.MyFloat
SELECT @.MyReal AS MyReal, @.MyFloat AS MyFloat
Hope this helps.
Dan Guzman
SQL Server MVP
"Blond Moment" <anonymous@.discussions.microsoft.com> wrote in message
news:14c101c50452$95db8c30$a601280a@.phx.gbl...
> Are there any conversion issues I should know when
> converting a real into a float ?
> Thanks
> J|||Mike,
Real and float are not the same data type, though they are in
the same data type family. The capabilities of the real and
float types in SQL Server derive from the internal format
used for their representation: real stores floating point values
according to the 4-byte IEEE standard, and float uses the
8-byte IEEE format.
Both real and float represent values exactly, but the reason they
are called "approximate" data types is that the exact values they
store do not match the decimal fixed-point values we use in the
familiar base-10 number system. Real and float store values exactly
in base-2 representation, so for example both real and float can store
11.5625 (or 11 9/16) exactly, but neither can store the value 0.1
(or 1/10) exactly, and an assignment of 1/10 to a real or float will
result in a stored value near to, but not equal to, 1/10.
Floats can store a wider range and precision than reals, but
the IEEE 4-byte and 8-byte formats are designed in such a
way that every representable real can be represented exactly
as a float.
All that said, the bottom line is that if a real value is assigned
to a float, no loss of precision will occur. It is similar to assigning
a decimal(5,2) value to a decimal(10,4).
SQL Server's use of "exact" and "approximate" to describe
decimal and float/real types is misleading, in my opinion. Both
type families store exact values. The values a decimal type like
decimal(3,1) can store are evenly spaced base-10 fixed-precision
numbers (-99.9, -99.8, ..., 99.8, 99.9), wherease the values a
float can store are unevenly-spaced base-2 floating-precision
numbers of the form (mantissa) * 2^(exponent), for a set of
evenly-spaced (mantissa) values >= 1/2 and < 1 and all values
of (exponent) between -1023 and +1023 (omitting some details
about the representation of infinity, negative zero, and reduced-
precision "denormalized" values with absolute value less
than 2^(-1023)).
-- Steve Kass
-- Drew University
-- Ref: E46D858E-5EFC-44C0-9EEB-F97C291E1B0D
Mike Epprecht (SQL MVP) wrote:

>Hi
>A real and a float are the same data type, just different names. Both are
>approximate and can not represent the exact value.
>Regards
>--
>Mike Epprecht, Microsoft SQL Server MVP
>Zurich, Switzerland
>IM: mike@.epprecht.net
>MVP Program: http://www.microsoft.com/mvp
>Blog: http://www.msmvps.com/epprecht/
>"Blond Moment" <anonymous@.discussions.microsoft.com> wrote in message
>news:14c101c50452$95db8c30$a601280a@.phx.gbl...
>
>
>