WN

WN (https://www.wn.se/forum/index.php)
-   Serversidans teknologier (https://www.wn.se/forum/forumdisplay.php?f=4)
-   -   Komma och decimaler i MySQL (https://www.wn.se/forum/showthread.php?t=15252)

hnn 2006-07-26 08:38

Har ett litet/stort problem med MySQL...

Har en tabell där priser lagras i en DOUBLE(8,2).

Problemet är när MySQL returnerar detta till kundens system som hanterar priserna ören med ,
Jag vet att det kommer bli problem i SQL vid inmatning, men man borde kunna få värdet returnerat som man vill...

Har letat i MySQLs manual, men hittar inte.

Berörda MySQL versioner: 4.1.13a-osx-ppc och 4.1.20-community-max

--

Visst, skulle kunna bygga om kundens system, men jag tycker att det borde vara ett relativt vanligt problem vid överföringar från kundsystem <-> MySQL.

Exempel:
Kod:

+---------+--------------+
| Pris  | Förväntat |
+---------+--------------+
| 4.00  |  4,00    |
+---------+--------------+
| 41.10  |  41,10  |
+---------+--------------+
| 411.05 |  411,05  |
+---------+--------------+


hnn 2006-07-26 11:06

Det får helt enkelt bli

Kod:

SELECT REPLACE(`Pris`, '.', ',') AS Pris FROM `priser`

lloigor 2006-07-30 00:40

Citat:

Originally posted by hnn@Jul 26 2006, 08:38
Har ett litet/stort problem med MySQL...

Har en tabell där priser lagras i en DOUBLE(8,2).

Borde antagligen använda DECIMAL(8,2) för att lagra priser... Det hjälper inte med att få ett komma istället för en punkt men däremot med att inte få fel siffror.

http://www2.hursley.ibm.com/decimal/...1.html#inexact

koala 2006-07-30 05:06

Hm, intressant. Jag har precis, utan att tänka så mycket på det, ändrat från decimal till double i min webbshop. Får försöka läsa den där artikeln (nu fungerade den inte) och kanske ändra tillbaka...

lloigor 2006-07-30 13:40

Klipper in en snutt av artikeln

Citat:

Binary floating-point cannot exactly represent decimal fractions, so if binary floating-point is used it is not possible to guarantee that results will be the same as those using decimal arithmetic. This makes it extremely difficult to develop and test applications that use exact real-world data, such as commercial and financial values.
...
For example, using the Java or C double datatype, 0.1 × 8 (a binary multiple) gives the result 0.800000000000000044408920985006261616945266723632 8125 but 0.1 added to itself 8 times gives the different answer 0.799999999999999933386618522490607574582099914550 78125. The two results would not compare equal, and further, if these values are multiplied by ten and rounded to the nearest integer below (the ‘floor’ function), the result will be 8 in one case and 7 in the other.

så decimal eller t.om. att räkna pengar i heltals-ören är bättre. Det blir små pengar oftast, om man inte har ohyggligt mycket transaktioner, men det räcker ju för att sabba bokföringen.


Alla tider är GMT +2. Klockan är nu 12:48.

Programvara från: vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Svensk översättning av: Anders Pettersson