2014-02-08 58 views
0
 
I have some Spec data(LSL/USL) something like this, 
with data type real, 
if I query with MSSQL Server Management Studio(MSSMS for short), 
it's displayed as 0.45, 
but if I query with php via sqlsrv driver for php4.4, 
I got 0.44999998807907, but I need to get it just exactly same in MSSMS, 
and it's not just rounding issue, 
ex: 
php => 0.00030000001424924, MSSMS => 0.0003 
php => 0.0049999998882413, MSSMS => 0.005 
php => 0.0049000000581145, MSSMS => 0.0049 

和!!!!上面的例子是同一列,所以不能通過簡單地應用於ROUND()來修復。如何從數據類型爲real列可讀價值MSSQL

 
I know real is Approximate Numerics, 
And so it's reasonable to get this, 
but I need a workaround, 
we didn't have this issue when we use asp or vb, 
probably some implicit casting by data driver, 

anyway, any advice or comment is welcome. 
 
**First, I want say "Thank You" to all of you guys taking time to help me.** 
but this question probably more tricky actually, 
sorry I didn't show enough example to guide you to right way, 
DECLARE @TABLE TABLE(VALUE REAL) 
INSERT INTO @TABLE VALUES 
(0.00030000001424924),(0.0049999998882413),(0.0049000000581145), 
(0.000099999997473788),(0.60000002384186),(0.000039999998989515), 
(0.00025000001187436) 

SELECT CAST(VALUE AS DECIMAL(10,4)) AS [Decimal Values] 
     ,ROUND(VALUE, 4)    AS [Rounded values] 
     ,CAST(VALUE AS NUMERIC(10,4)) AS [Numeric Values] 
FROM @TABLE 
 
in above case, 
0.00025000001187436 should be 0.00025 in MSSMS, but we will get 0.0003 
0.000039999998989515 should be 4E-05. 

How do we simulate and get same result in PHP query? 
Is that possible? 

And, What if I decide change the datatype from real to decimal, 
what is correct precision for decimal? decimal(?, ?) 
to not losing precision. 
+0

? 「0.005」可以「讀」爲「0.0050」嗎?如果是這樣,則只需使用比例爲4的「DECIMAL」,例如'CONVERT(DECIMAL(9,4),col)'。如果您希望顯示的小數位數取決於更高位數字後的前0位,請在表示層中進行此格式設置。 –

回答

0

有一對夫婦的做這件事的方式之一爲阿龍建議和其他一些如下

測試數據

DECLARE @TABLE TABLE(VALUE REAL) 
INSERT INTO @TABLE VALUES 
(0.00030000001424924),(0.0049999998882413),(0.0049000000581145) 

查詢

SELECT CAST(VALUE AS DECIMAL(10,4)) AS [Decimal Values] 
     ,ROUND(VALUE, 4)    AS [Rounded values] 
     ,CAST(VALUE AS NUMERIC(10,4)) AS [Numeric Values] 
FROM @TABLE 

結果你在第一時間使用上的目的REAL設置

╔════════════════╦════════════════╦════════════════╗ 
║ Decimal Values ║ Rounded values ║ Numeric Values ║ 
╠════════════════╬════════════════╬════════════════╣ 
║ 0.0003   ║ 0.0003   ║ 0.0003   ║ 
║ 0.0050   ║ 0.005   ║ 0.0050   ║ 
║ 0.0049   ║ 0.0049   ║ 0.0049   ║ 
╚════════════════╩════════════════╩════════════════╝ 
相關問題