Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numeric/Decimal type return as float #257

Open
badmansan opened this issue May 9, 2020 · 2 comments
Open

Numeric/Decimal type return as float #257

badmansan opened this issue May 9, 2020 · 2 comments

Comments

@badmansan
Copy link

@badmansan badmansan commented May 9, 2020

Version: v3.0.5

Bug Description

Numeric/Decimal type return as float instead string

Steps To Reproduce

-- mysql
CREATE TABLE `balance` (
  `id` int(11) NOT NULL,
  `balance` decimal(12,4) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `balance` (`id`, `balance`) VALUES
(1,	12.1234);

-- postgresql
CREATE TABLE "public"."balance" (
    "id" integer NOT NULL,
    "balance" numeric(12,4) NOT NULL
) WITH (oids = false);
INSERT INTO "balance" ("id", "balance") VALUES
(1,	12.1234);
<?php
$pdo = new PDO('pgsql:host=localhost;dbname=test', 'postgres', 'postgres');
//$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->prepare('SELECT balance FROM balance WHERE id = ?');
$stmt->execute([1]);
var_dump($stmt->fetchColumn());

$db = new Nette\Database\Connection('pgsql:host=localhost;dbname=test', 'postgres', 'postgres');
//$db = new Nette\Database\Connection('mysql:host=localhost;dbname=test', 'root', 'root');

$res = $db->query('SELECT balance FROM balance WHERE id = ?', 1);
var_dump($res->fetchField());

after run php code we got

(string) '12.1234'
(float) 12.1234

Expected Behavior

(string) '12.1234'
(string) '12.1234'

Possible Solution

I think float must return only if db field type is float or double

@dg
Copy link
Member

@dg dg commented May 10, 2020

Why should it return decimal as a string?

@badmansan
Copy link
Author

@badmansan badmansan commented May 10, 2020

Because these types are generally used for storing money, and we never use floats for money. Php does not have a built-in money format, so the only way out is a string. At first example with PDO you can see the right way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.