Having trouble storing dollar amounts in the thousands due to the "," that gets inserted once the number becomes 1, 000.00. Any advise?

Dani AI

Generated

Short, practical notes tied to the replies here.

As discovered, the root cause is applying display formatting before the insert. Thousands separators or currency symbols turn the input into a non‑pure numeric string; MySQL will stop parsing at the first non‑numeric character, so you get wrong/truncated values. ’s idea of replacing separators can work, but it’s safer and more predictable to sanitize in the application layer and keep formatting strictly for presentation.

Use a fixed‑point column for money (not FLOAT). Pick precision to cover your largest amounts; M is total digits and D is decimals, so DECIMAL(M,2) gives you M−2 integer digits plus two cents. Example schema:

amount DECIMAL(12,2) NOT NULL DEFAULT '0.00'

Sanitize input before sending to SQL and bind parameters (avoid calling number_format() before storage). Example PHP flow (illustrative):

$raw = $_POST['amount'];      // e.g. "$1,234.56"
$clean = preg_replace('/[^\d\.\-]/', '', $raw); // "1234.56"
$clean = preg_replace('/(\..*)\./', '$1', $clean); // remove extra dots
$stmt = $pdo->prepare("INSERT INTO payments (amount) VALUES (:amount)");
$stmt->execute([':amount' => $clean]);

Alternative: store amounts as integer cents to avoid any decimal handling:

$cents = (int) round((float)$clean * 100);
INSERT INTO payments (amount_cents) VALUES (?);

Quick troubleshooting checklist: confirm the bound value (log it) before execute, verify the column type is DECIMAL (or integer cents), and keep number_format() or locale formatting for display code only. This approach prevents the comma/thousands‑separator issue and keeps values accurate over time.

Recommended Answers

All 2 Replies

Filter the "." before you insert it. You can replace the period by applying replace():

INSERT INTO mytable myvalue VALUES (replace('1.000','.',''));

I appreciate your advise. However this turned out to be a beginner's mistake. I was using number_format() "BEFORE" my insert statement. That was throwing the "," in there. I just took off the number_format(), and used decimal(10,2) and it works perfectly. Sorry. haha lesson learned :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.