-1

I have table with real column, i need to multiple all row with billion , this give me good result(151112000):

declare @tbl table (n real)
insert into @tbl values(151.112)
select cast (n * 1000000 as float) AS [Quantity1]
from @tbl

But when i select from old existing table, 151.112 became to 151112048.

How it can be?

My old ddl table:

CREATE TABLE [dbo].[ProvidentSegmentPortfolioMonthlyHistory](
    [Fund_id] [int] NOT NULL,
    [Report_Date] [datetime] NOT NULL,
    [Segment_id] [int] NOT NULL,
    [Quantity] [real] NULL,
    [QuantityPercent] [real] NULL,
    [Date_Update] [datetime] NULL,
    [operator] [varchar](50) NULL,
 CONSTRAINT [PK_ProvidentSegmentPortfolioMonthlyHistory ] PRIMARY KEY CLUSTERED 
(
    [Fund_id] ASC,
    [Report_Date] ASC,
    [Segment_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
yantrab
  • 2,482
  • 4
  • 31
  • 52

1 Answers1

2

I understand that is floating point issue, but i don't understand why it is different from oldest table.

It's different because you think the old table has a value of 151.112 in it, but it doesn't. It has a value of 151.112048 in it, which it displays as 151.112 because of floating data point inaccuracy.

How to resolve it is to use Decimal instead of Real in your table.

Tab Alleman
  • 31,483
  • 7
  • 36
  • 52