Dev.to WebDev 🛠 Dev 👁 0 📖 6 min read

Typing 4500000.5 or 4500000,5 into our yen field gave 45,000,005, ten times too much, in 9 of 14 replayed cases

A salary calculator of ours turned the text 4500000.5 into 45,000,005 yen and showed a take-home figure for a salary ten times bigger than the one typed. It did this with no warning, in 9 of the 14 cases the agents repla

Typing 4500000.5 or 4500000,5 into our yen field gave 45,000,005, ten times too much, in 9 of 14 replayed cases

A salary calculator of ours turned the text 4500000.5 into 45,000,005 yen and showed a take-home figure for a salary ten times bigger than the one typed. It did this with no warning, in 9 of the 14 cases the agents replayed through the first version of the field: two ways of typing a half yen (a dot, 4500000.5, and a comma, 4500000,5), in each of our seven page languages. The dot failed in 2 of the 7 languages and the comma in all 7. After one commit that rewrote both the input code and the validator it happened in 0 of 14, and all 14 now ask for a whole yen amount instead.

I run an experiment: AI coding agents build and test a set of tools for people living in Japan, and I direct them and check what they report. I did not write this code by hand. The page that matters here is the net salary calculator, part of Japan Life Hub: you type your yearly pay before tax in yen and it shows what lands in your account each month after tax, pension and health insurance. You can try the field yourself there.

What the field used to do

The amount boxes in these calculators were <input type="number">. A salary of 5000000 showed as raw digits, which is hard to read, so the agents replaced it with a text field that groups digits as you type: 5,000,000 in English, 5.000.000 in Vietnamese and Indonesian, 50,00,000 in Hindi (the Indian system, with a group of two digits). The phone gets the number pad through inputMode="numeric".

Just before that, the agents had found that eight of the calculators dropped the result without a word when the amount was empty, zero or negative, and accepted 99999999999 or 4500000.5 as if they were fine. They had added parseYenAmount, which reads a whole amount from 1 to 100,000,000 yen and otherwise puts one line under the field, in the page's language: "Enter {field} in yen as a whole amount from ¥1 to ¥{maximum}."

Two pieces of code were involved: one reformats what you type, the other decides whether the result is a valid amount, and both caused the comma failure. The first validator (commit 24494b4e) ran replace(/[¥,\s]/g, "") before reading the number, and the first formatter (2850739b, stripYenInputCharacters) ran replace(/[¥,]/g, ""), so each dropped every comma in every language and a typed 4500000,5 turned into 45000005 before any check. That is 7 of the 9 failures. The other 2 of the 9 are the dot, which sits in the formatter: on the id and vi pages, where the dot is the group separator, it removed every dot, including the ones the visitor typed, because it could not tell the separator it had put there from one you had just typed. The en, ja, ru, hi and zh pages kept 4500000.5 and showed the line.

The dot that means two things

In English, the separator between thousands is a comma and a dot is a decimal point. In Vietnamese and Indonesian it is the other way round: the dot groups the thousands. Our formatter asks the browser which separator the page language uses:

// shortened from yen-input.ts; the comments marked "added" are ours, not in the original
function yenGrouping(locale: string) {
  const numberLocale = locale === "hi" ? "en-IN" : locale;   // added: Hindi gets Indian grouping, 50,00,000
  const parts = new Intl.NumberFormat(numberLocale, { maximumFractionDigits: 0 })
    .formatToParts(123456789012345);
  const separator = parts.find((part) => part.type === "group")?.value;
  // added: groups = sizes of the integer chunks, e.g. [3, 3, 3, 3, 3] or [3, 2, 2, 2, 2, 2]
  return { groups: parts.filter((p) => p.type === "integer").map((p) => p.value.length), separator };
}

In id and vi that separator is .. When you type, the code removes the separator and puts it back in the right places, so the field can show 4.500.000 and keep the digits 4500000 underneath.

So you type 4500000.5 into a Vietnamese page. The field removes the dot, keeps 45000005, and draws it as 45.000.005. The validator sees a whole number between 1 and 100,000,000 and is satisfied. The calculator does the rest, for a salary ten times too big.

The agents' own live check found the dot. The fix commit (9be3dd6e) gives 10 of 280 cases for the dot in id and vi and no count for the comma. Its title names 4500000,5 and the same commit fixes the decimal comma, while the replay below shows the first version failing on the comma in all 7 languages. I did not rerun the 280 and do not have the list; the replay uses a smaller set.

Replaying typing through both versions

I wanted the number to come from running the code. The two versions of yen-input.ts are in git, so a short script written for this article types a string one character at a time into each version, exactly as the component would call it: the previous value, the text the browser reports, the cursor at the end, and the option that keeps text that is not an amount so the warning can show. Then it asks parseYenAmount what the calculator would receive.

The inputs: 4500000.5, 4500000,5, 4,500,000, 4.500.000, -500000, 99999999999 and 4500000, in the seven page languages (en, ja, ru, hi, zh, id, vi). The agents count two of them as the failure: a half yen typed with a dot and a half yen typed with a comma. That is 14 cases.

A grid of 7 page languages by 2 typed inputs: the first version of the field gives 45,000,005 yen in 9 of 14 cells, the current version shows the warning line in all 14

On the chart, the bottom two rows are the current version, where the line under the field shows.

Typed First version: wrong amount Current version: wrong amount
4500000.5 2 of 7 languages (id, vi) 0 of 7
4500000,5 7 of 7 languages 0 of 7
Total 9 of 14 0 of 14

The other five inputs behaved the same in both versions, apart from the one I come back to at the end. -500000 and 99999999999 were never taken for amounts, and a plain 4500000 was read as 4,500,000 everywhere (replay rerun on 27.11).

The 9 of 14 counts the cases the agents chose for the replay, and I have no data on how often a stray dot or comma lands in this field. The replay covers typing only; pasting goes down a different path.

By commit times, which are not deploy times, the first formatter (2850739b) went in on 24.11 at 14:15Z and the fix (9be3dd6e, after the agents' live check) at 14:46Z, 31 minutes later.

The fix: compare with what the field showed

The rewrite stopped guessing which characters were separators. It compares what the browser now reports with what the field had displayed before the edit, finds the part that changed, and treats everything outside that part as the field's own text:

// shortened from yen-input.ts; comments added
function findEdit(displayed: string, raw: string) {
  let prefix = 0;                       // characters unchanged at the start
  while (prefix < Math.min(displayed.length, raw.length) && displayed[prefix] === raw[prefix]) prefix++;
  let suffix = 0;                       // characters unchanged at the end
  while (suffix < Math.min(displayed.length, raw.length) - prefix
      && displayed[displayed.length - 1 - suffix] === raw[raw.length - 1 - suffix]) suffix++;
  return { inserted: raw.slice(prefix, raw.length - suffix) };
}

Only separators in the unchanged prefix and suffix, which the field itself put there, are removed. What the visitor inserted keeps its dots and commas, unless it is a well-formed grouped amount, such as a pasted 5.000.000 in Vietnamese or 5,000,000 anywhere. The validator also changed: a comma is now accepted only as a thousands separator, with this check:

// from amount-validation.ts; the comment after the regex is added
/^(?:\d{1,3}(,\d{3})+|\d{1,2}(,\d{2})*,\d{3})$/   // 4,500,000 and 45,00,000 pass; 4500000,5 does not

The second alternative is there for Indian grouping. A stray .5 or ,5 now stays in the box, fails the whole-yen test, and the line under the field says what to type.

The field is live in the net salary calculator.

The question I am left with

The replay has one input that got worse, outside the table and chart: 4.500.000 typed on the id and vi pages. The field cannot tell whether that typed dot is a thousands separator or a decimal point, and a hand-typed dot is kept as typed, so the new rule refuses it with the line under the field. The first version accepted it as 4,500,000, which is what a Vietnamese reader meant. A pasted 4.500.000 still works. On those two pages a typed 4,500,000 (a comma, which is their decimal mark) is accepted in both versions.

The fix trades a silent amount ten times too big for a warning. I have no number for how many people type their dots in those two languages, and the agents did not collect one. When a dot is both the thousands mark and what some visitors use as a decimal point, should a text field accept a typed 4.500.000 and guess, or refuse and explain?

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.