敷金の精算(返ってくる額の目安)

2進小数の誤差0.5 の丸め方

言語ごとの結果

正解表 35件のうち、正解と一致した件数です。

PHPPHP 8.4.26

よくある書き方26 / 35

直した書き方35 / 35

PythonPython 3.14.7

よくある書き方26 / 35

直した書き方35 / 35

GoGo 1.27.2

よくある書き方26 / 35

直した書き方35 / 35

JavaScriptNode 24.15.0

よくある書き方24 / 35

直した書き方35 / 35

JavaJava 21.0.12

よくある書き方24 / 35

直した書き方35 / 35

C#.NET 10.0.402

よくある書き方26 / 35

直した書き方35 / 35

RustRust 1.99.0

よくある書き方26 / 35

直した書き方35 / 35

RubyRuby 4.0.7

よくある書き方26 / 35

直した書き方35 / 35

KotlinKotlin 2.4.21

よくある書き方24 / 35

直した書き方35 / 35

SwiftSwift 6.3.3

よくある書き方26 / 35

直した書き方35 / 35

なぜ割れたのか

よくある書き方が落ちたケースを、原因ごとにまとめた解説です(2本)。書いてある「落ちた言語」が今回の検証結果と一致することを確かめてから載せています。

2進小数の誤差

万円のまま掛けたら、8.29万円の12.5%(10,362.5円)を10言語すべてが 10,362円にした

対象のケースc005c006c009c010c011c016 落ちた言語PHPPythonGoJavaScriptJavaC#RustRubyKotlinSwift

敷金の精算では、原状回復費の総額に借主の負担割合を掛けます。元ツールは金額を「万円」で受け取り、万円のまま掛けてから × 10000 で円にして四捨五入します。原状回復費 8.29万円・負担 12.5%(c005)なら、正しくは 82,900円 × 12.5% = 10,362.5円で、四捨五入すると 10,363円です。よく見かける書き方では、10言語すべてが 10,362円を返しました。

言語よく見かける書き方負担分(c005)
10言語すべてround(8.29 * 12.5 / 100 * 10000)10,362

8.29 は2進数の小数でぴったり表せないので、途中の値が 0.5 のわずかに下になります。

$ node -e "console.log(8.29 * 12.5 / 100 * 10000)"
10362.499999999998

丸める前の値が 0.5 より小さいので、どの言語の丸め関数でも下に落ちます。0.5 を偶数へ丸めるか、0 から遠い方へ丸めるかの違いは関係ありません。

内訳と合計が合わなくなる

c010 は、同じ原状回復費にクリーニング3万円を足したケースです。負担の合計は、項目ごとではなく万円の合計(4.03625万円)を × 10000 して丸めます。こちらは 40,362.5 ちょうどになり、8言語は 40,363円に切り上げました。

項目正解Go・Java・JavaScript・Kotlin・PHP・Ruby・Rust・Swift
原状回復の負担分10,36310,362
クリーニング30,00030,000
負担の合計40,36340,363

内訳を足すと 40,362円なのに、合計は 40,363円です。画面に並べると、見た人が計算の合わない明細を受け取ります。

3円の原状回復費の50%(c016)も、1.5円が 1.4999… になって 1円になりました。

どう直したか

金額の文字列を円の整数(小数第4位までの万円なので必ず整数)に、負担割合を100倍の整数にして、整数だけで計算しました。

負担分 = (2 × 原状回復費(円) × 負担割合の100倍 + 10000) ÷ 20000 (整数の割り算)

丸めるのはこの1回だけで、合計は足し算、返金は引き算です。10言語とも同じ形で書けて、35件中35件になりました。

0.5 の丸め方

返金を別に丸めたら、負担と足して敷金より1円多くなった(マイナスの 0.5 は Java・JavaScript・Kotlin が 0 の側へ)

対象のケースc003c004c007c008c015 落ちた言語PHPPythonGoJavaScriptJavaC#RustRubyKotlinSwift

返ってくる敷金は「敷金 − 借主の負担の合計」です。元ツールは、負担の合計と返金をそれぞれ別に、万円の小数から丸めます。敷金10万円・原状回復費 5.25万円・負担 12.5%(c003)では、負担は 6,562.5円です。

  • 負担: 6,562.5円を四捨五入して 6,563円
  • 返金: 100,000 − 6,562.5 = 93,437.5円を四捨五入して 93,438円

足すと 100,001円で、預けた敷金より1円多くなります。0.5円の位置では、負担が切り上がると、返金も同時に切り上がるためです。10言語すべてで、返金が 93,438円になりました(正しくは 100,000 − 6,563 = 93,437円)。

敷金1万円・原状回復費1円・負担50%(c015)でも同じです。負担 0.5円を 1円に、返金 9,999.5円を 10,000円に切り上げて、足すと 10,001円になりました(Python と C# は負担を 0円にしました)。

偶数へ丸める Python と C#

Python の round と C# の Math.Round は、何も指定しないとちょうど半分を偶数の側へ丸めます。c003 では負担が 6,562円、返金が 93,438円になり、足すと 100,000円に戻りました。ただし、負担分そのものが1円少ない答えです。

c004(5.15万円・12.5%)は、負担が 6,437.5円、返金が 93,562.5円です。偶数へ丸めると 6,438円と 93,562円になり、Python と C# は正解でした。ほかの8言語は返金を 93,563円にしました。偶数へ丸める関数が正しいのではなく、偶数の位置がたまたま合っただけです。

敷金で足りないとき

敷金0円(c007・c008)では、返金がマイナス(追加で払う額)になります。c008 は負担が 6,437.5円なので、返金は -6,437.5円から丸めます。

言語よく見かける書き方返金(c008)
Java・KotlinMath.round・roundToLong()-6,437
JavaScriptMath.round-6,437
Go・PHP・Ruby・Rust・Swift0 から遠い方へ-6,438
Python・C#偶数へ-6,438

Java・Kotlin・JavaScript の丸めは、ちょうど半分を「大きい方(+ の側)」へ寄せます。マイナスでは 0 に近い方になるので、追加で払う額が1円少なくなりました。

どう直したか

丸めるのは原状回復の負担分の1回だけにして、返金は丸めずに引き算で出しました。

負担の合計 = 負担分(丸めたあとの整数) + クリーニング + 未払い + その他
返金       = 敷金(円) − 負担の合計

返金が整数どうしの引き算になるので、マイナスの 0.5 を丸める場面がなくなります。負担と返金を足すと必ず敷金に戻り、10言語とも35件中35件になりました。

正解表と、よくある書き方の答え

正解は実装より先に決めて固定しています。右の欄は、よくある書き方の答えを言語ごとにまとめたものです(赤い値は実際に返した間違った答え)。

ケース入力正解よくある書き方の答え(10言語)
c001基本・返ってくる(元ツールの検算例) 原状回復 8万円の50%で4万円、クリーニング3万円・未払い2万円で負担は9万円。返ってくるのは11万円
deposit
20
restore_total
8
restore_rate
50
cleaning
3
unpaid
2
other
0
restore_borne
40000
tenant_total
90000
refund
110000

✓10言語すべて正解

c002敷金では足りない(元ツールの検算例) 負担は11万円なので、返金は -6万円(6万円の追加の支払い)
deposit
5
restore_total
6
restore_rate
100
cleaning
3
unpaid
2
other
0
restore_borne
60000
tenant_total
110000
refund
-60000

✓10言語すべて正解

c003原状回復費の負担分がちょうど 0.5円(2進数の小数でもぴったり・整数部が偶数) 52,500円の12.5%は 6,562.5円 → 6,563円。偶数へ丸める関数だと 6,562円。返金は 100,000 − 6,563 = 93,437円で、返金を 93,437.5 から別に丸めると 93,438円になり、負担と足して 100,001円になる なぜ? 解説を読む
deposit
10
restore_total
5.25
restore_rate
12.5
cleaning
0
unpaid
0
other
0
restore_borne
6563
tenant_total
6563
refund
93437
  • PHP・Go・JavaScript・Java・Rust・Ruby・Kotlin・Swiftrefund93438
  • Python・C#restore_borne6562tenant_total6562refund93438
c004原状回復費の負担分がちょうど 0.5円(2進数の小数でもぴったり・整数部が奇数) 51,500円の12.5%は 6,437.5円 → 6,438円。偶数へ丸める関数でも 6,438円 なぜ? 解説を読む
deposit
10
restore_total
5.15
restore_rate
12.5
cleaning
0
unpaid
0
other
0
restore_borne
6438
tenant_total
6438
refund
93562
  • PHP・Go・JavaScript・Java・Rust・Ruby・Kotlin・Swiftrefund93563
  • Python・C#✓ 正解
c005原状回復費の負担分がちょうど 0.5円(2進数の小数では少し小さくなる) 82,900円の12.5%は 10,362.5円 → 10,363円。万円の小数のまま 8.29 × 12.5 ÷ 100 × 10000 と2進数の小数で計算すると 10,362.499… になる なぜ? 解説を読む
deposit
10
restore_total
8.29
restore_rate
12.5
cleaning
0
unpaid
0
other
0
restore_borne
10363
tenant_total
10363
refund
89637
  • PHP・Python・Go・JavaScript・Java・C#・Rust・Ruby・Kotlin・Swiftrestore_borne10362tenant_total10362refund89638
c006負担割合 37.5% で、2進数の小数では少し小さくなる 61,100円の37.5%は 22,912.5円 → 22,913円 なぜ? 解説を読む
deposit
10
restore_total
6.11
restore_rate
37.5
cleaning
0
unpaid
0
other
0
restore_borne
22913
tenant_total
22913
refund
77087
  • PHP・Python・Go・JavaScript・Java・C#・Rust・Ruby・Kotlin・Swiftrestore_borne22912tenant_total22912refund77088
c007敷金0円で、足りない額がちょうど 0.5円の位置(整数部が偶数) 返金は 0 − 6,563 = -6,563円。返金を -6,562.5 から丸めると、大きい方へ丸める関数は -6,562円、偶数へ丸める関数も -6,562円になる なぜ? 解説を読む
deposit
0
restore_total
5.25
restore_rate
12.5
cleaning
0
unpaid
0
other
0
restore_borne
6563
tenant_total
6563
refund
-6563
  • Python・C#restore_borne6562tenant_total6562refund-6562
  • JavaScript・Java・Kotlinrefund-6562
  • PHP・Go・Rust・Ruby・Swift✓ 正解
c008敷金0円で、足りない額がちょうど 0.5円の位置(整数部が奇数) 返金は -6,438円。-6,437.5 から丸めると、大きい方へ丸める関数は -6,437円 なぜ? 解説を読む
deposit
0
restore_total
5.15
restore_rate
12.5
cleaning
0
unpaid
0
other
0
restore_borne
6438
tenant_total
6438
refund
-6438
  • JavaScript・Java・Kotlinrefund-6437
  • PHP・Python・Go・C#・Rust・Ruby・Swift✓ 正解
c009敷金0円で、2進数の小数では少し小さくなる 返金は -10,363円 なぜ? 解説を読む
deposit
0
restore_total
8.29
restore_rate
12.5
cleaning
0
unpaid
0
other
0
restore_borne
10363
tenant_total
10363
refund
-10363
  • PHP・Python・Go・JavaScript・Java・C#・Rust・Ruby・Kotlin・Swiftrestore_borne10362tenant_total10362refund-10362
c010返金を別に丸めると、負担の合計と足して敷金にならない 負担は 10,363 + 30,000 = 40,363円で、返金は 50,000 − 40,363 = 9,637円。元ツールの式(万円の2進数の小数・項目ごとに丸める)だと、原状回復の負担分 10,362円・負担の合計 40,363円・返金 9,638円になり、内訳を足しても合計にならず、合計と返金を足すと 50,001円になる なぜ? 解説を読む
deposit
5
restore_total
8.29
restore_rate
12.5
cleaning
3
unpaid
0
other
0
restore_borne
10363
tenant_total
40363
refund
9637
  • PHP・Go・JavaScript・Java・Rust・Ruby・Kotlin・Swiftrestore_borne10362refund9638
  • Python・C#restore_borne10362tenant_total40362refund9638
c011原状回復の 0.5円とクリーニング 0.5万円の足し算 負担の合計は 10,363 + 5,000 = 15,363円 なぜ? 解説を読む
deposit
10
restore_total
8.29
restore_rate
12.5
cleaning
0.5
unpaid
0
other
0
restore_borne
10363
tenant_total
15363
refund
84637
  • PHP・Python・Go・JavaScript・Java・C#・Rust・Ruby・Kotlin・Swiftrestore_borne10362tenant_total15362refund84638
c012負担割合 0%
deposit
20
restore_total
8
restore_rate
0
cleaning
3
unpaid
0
other
0
restore_borne
0
tenant_total
30000
refund
170000

✓10言語すべて正解

c013負担割合 100%
deposit
20
restore_total
8
restore_rate
100
cleaning
0
unpaid
0
other
0
restore_borne
80000
tenant_total
80000
refund
120000

✓10言語すべて正解

c014負担割合が小数第2位まで(33.33%) 100,001円の33.33%は 33,330.3333円 → 33,330円
deposit
20
restore_total
10.0001
restore_rate
33.33
cleaning
0
unpaid
0
other
0
restore_borne
33330
tenant_total
33330
refund
166670

✓10言語すべて正解

c0151円の原状回復費の50%(0.5円) 0.5円 → 1円 なぜ? 解説を読む
deposit
1
restore_total
0.0001
restore_rate
50
cleaning
0
unpaid
0
other
0
restore_borne
1
tenant_total
1
refund
9999
  • PHP・Go・JavaScript・Java・Rust・Ruby・Kotlin・Swiftrefund10000
  • Python・C#restore_borne0tenant_total0refund10000
c0163円の原状回復費の50%(1.5円) 1.5円 → 2円。2進数の小数では 1.4999… になる なぜ? 解説を読む
deposit
1
restore_total
0.0003
restore_rate
50
cleaning
0
unpaid
0
other
0
restore_borne
2
tenant_total
2
refund
9998
  • PHP・Go・JavaScript・Java・Rust・Ruby・Kotlin・Swiftrestore_borne1tenant_total1refund9999
  • Python・C#restore_borne1tenant_total1
c0171円の原状回復費の0.01% 0.0001円 → 0円
deposit
1
restore_total
0.0001
restore_rate
0.01
cleaning
0
unpaid
0
other
0
restore_borne
0
tenant_total
0
refund
10000

✓10言語すべて正解

c018小さな金額の足し算(0.1万円 + 0.2万円 + 0.3万円) 負担は 6,000円、返金は 4,000円
deposit
1
restore_total
0
restore_rate
100
cleaning
0.1
unpaid
0.2
other
0.3
restore_borne
0
tenant_total
6000
refund
4000

✓10言語すべて正解

c019万円の小数第4位まで(1円単位) 負担は 12,345 + 1 + 9 = 12,355円
deposit
12.3456
restore_total
0
restore_rate
100
cleaning
1.2345
unpaid
0.0001
other
0.0009
restore_borne
0
tenant_total
12355
refund
111101

✓10言語すべて正解

c020返金がちょうど 0円
deposit
11
restore_total
8
restore_rate
50
cleaning
3
unpaid
2
other
2
restore_borne
40000
tenant_total
110000
refund
0

✓10言語すべて正解

c021同じ金額の別の書き方(8.0・3.0000・020) c001 と同じ答え
deposit
020
restore_total
8.0
restore_rate
50
cleaning
3.0000
unpaid
2
other
0
restore_borne
40000
tenant_total
90000
refund
110000

✓10言語すべて正解

c022同じ割合の別の書き方(50.00) c001 と同じ答え
deposit
20
restore_total
8
restore_rate
50.00
cleaning
3
unpaid
2
other
0
restore_borne
40000
tenant_total
90000
refund
110000

✓10言語すべて正解

c023どの金額も上限・負担割合100% 負担は4億円、返金は -3億円
deposit
10000
restore_total
10000
restore_rate
100
cleaning
10000
unpaid
10000
other
10000
restore_borne
100000000
tenant_total
400000000
refund
-300000000

✓10言語すべて正解

c024敷金が上限で負担は0円
deposit
10000
restore_total
0
restore_rate
0
cleaning
0
unpaid
0
other
0
restore_borne
0
tenant_total
0
refund
100000000

✓10言語すべて正解

c025金額が上限を超える(1億円と1円)
deposit
10000.0001
restore_total
0
restore_rate
0
cleaning
0
unpaid
0
other
0
error
invalid_input

✓10言語すべて正解

c026負担割合が100%を超える
deposit
20
restore_total
8
restore_rate
100.01
cleaning
0
unpaid
0
other
0
error
invalid_input

✓10言語すべて正解

c027負担割合が負
deposit
20
restore_total
8
restore_rate
-5
cleaning
0
unpaid
0
other
0
error
invalid_input

✓10言語すべて正解

c028金額が負
deposit
-1
restore_total
8
restore_rate
50
cleaning
0
unpaid
0
other
0
error
invalid_input

✓10言語すべて正解

c029金額が小数第5位まである
deposit
20
restore_total
1.00001
restore_rate
50
cleaning
0
unpaid
0
other
0
error
invalid_input

✓10言語すべて正解

c030負担割合が小数第3位まである
deposit
20
restore_total
8
restore_rate
12.555
cleaning
0
unpaid
0
other
0
error
invalid_input

✓10言語すべて正解

c031金額が指数の形
deposit
1e3
restore_total
8
restore_rate
50
cleaning
0
unpaid
0
other
0
error
invalid_input

✓10言語すべて正解

c032金額の小数点の前が無い
deposit
20
restore_total
.5
restore_rate
50
cleaning
0
unpaid
0
other
0
error
invalid_input

✓10言語すべて正解

c033金額の小数点の後ろが無い
deposit
20
restore_total
5.
restore_rate
50
cleaning
0
unpaid
0
other
0
error
invalid_input

✓10言語すべて正解

c034金額が数でない
deposit
20
restore_total
8
restore_rate
50
cleaning
abc
unpaid
0
other
0
error
invalid_input

✓10言語すべて正解

c035金額が空
deposit
restore_total
8
restore_rate
50
cleaning
0
unpaid
0
other
0
error
invalid_input

✓10言語すべて正解

コード

「違い」では、よくある書き方から直した書き方へ、どの行をどう変えたかを示しています(赤が消した行、緑が足した行、濃い色が行の中で変わった部分)。計算の部分だけを載せ、入出力を受け渡す部分は除いています。

Deposit.php+46 −24よくある書き方(5251fc1)→ 直した書き方

@@ 変更前 3行目〜 / 変更後 3行目〜 @@
33
44 /**
55 * 敷金の精算(返ってくる額の目安)。
6− * v1: 形と範囲を確かめたあと (float) に変換し、元ツールと同じ順に 万円 のまま掛けて足して引き、
7− * 負担分・負担の合計・返金をそれぞれ round(万円 × 10000) で円の整数にした。
6+ * v2: 金額は文字列のまま形を確かめて円の整数に、割合は100倍の整数にする。負担分は
7+ * intdiv(2 × 原状回復費(円) × 割合100倍 + 10000, 20000) で1回だけ四捨五入し(ちょうど半分は大きい方へ)、
8+ * 負担の合計は足し算、返金は 敷金(円) − 負担の合計 で丸めない。最大でも 2 × 10^12 で、64bit の int に収まる。
9+ * v1((float) に変換し、元ツールの順に万円のまま計算して3つを別々に round(万円 × 10000))は35件中26件だった。
10+ * 返金を別に丸めて 0.5 が1円多くなる(c003・c004・c015)、8.29 × 12.5 ÷ 100 × 10000 が 10362.499… になり
11+ * 負担分が1円少なくなる(c005・c006・c009・c010・c011・c016)。
812 */
913
10−const AMOUNT_FORMAT = '/^[0-9]+(?:\.[0-9]{1,4})?$/D';
11−const RATE_FORMAT = '/^[0-9]+(?:\.[0-9]{1,2})?$/D';
12−const AMOUNT_MAX = 10000;
13−const RATE_MAX = 100;
14+const AMOUNT_FORMAT = '/^([0-9]+)(?:\.([0-9]{1,4}))?$/D';
15+const RATE_FORMAT = '/^([0-9]+)(?:\.([0-9]{1,2}))?$/D';
16+const AMOUNT_MAX_YEN = 100000000; // 10000万円
17+const RATE_MAX_X100 = 10000; // 100%
1418
15−function parseField(mixed $v, string $format, float $max): ?float
19+/** "8.29" → 82900(円)。形が違う・範囲の外なら null */
20+function yenOf(mixed $v): ?int
1621 {
17− if (!is_string($v) || !preg_match($format, $v)) {
22+ if (!is_string($v) || !preg_match(AMOUNT_FORMAT, $v, $m)) {
1823 return null;
1924 }
20− $f = (float)$v;
21− return $f > $max ? null : $f;
25+ $int = ltrim($m[1], '0');
26+ if (strlen($int) > 5) { // とても長い数字で (int) があふれる前に落とす
27+ return null;
28+ }
29+ $yen = (int)$int * 10000 + (int)str_pad($m[2] ?? '', 4, '0');
30+ return $yen > AMOUNT_MAX_YEN ? null : $yen;
31+}
32+
33+/** "12.5" → 1250。形が違う・範囲の外なら null */
34+function rateX100(mixed $v): ?int
35+{
36+ if (!is_string($v) || !preg_match(RATE_FORMAT, $v, $m)) {
37+ return null;
38+ }
39+ $int = ltrim($m[1], '0');
40+ if (strlen($int) > 3) {
41+ return null;
42+ }
43+ $x100 = (int)$int * 100 + (int)str_pad($m[2] ?? '', 2, '0');
44+ return $x100 > RATE_MAX_X100 ? null : $x100;
2245 }
2346
2447 function settleDeposit(array $in): array
2548 {
26− $v = [];
27− foreach (['deposit', 'restore_total', 'restore_rate', 'cleaning', 'unpaid', 'other'] as $k) {
28− $v[$k] = $k === 'restore_rate'
29− ? parseField($in[$k] ?? null, RATE_FORMAT, RATE_MAX)
30− : parseField($in[$k] ?? null, AMOUNT_FORMAT, AMOUNT_MAX);
31− if ($v[$k] === null) {
32− return ['error' => 'invalid_input'];
33− }
49+ $deposit = yenOf($in['deposit'] ?? null);
50+ $restoreTotal = yenOf($in['restore_total'] ?? null);
51+ $rate = rateX100($in['restore_rate'] ?? null);
52+ $cleaning = yenOf($in['cleaning'] ?? null);
53+ $unpaid = yenOf($in['unpaid'] ?? null);
54+ $other = yenOf($in['other'] ?? null);
55+ if (in_array(null, [$deposit, $restoreTotal, $rate, $cleaning, $unpaid, $other], true)) {
56+ return ['error' => 'invalid_input'];
3457 }
35− $restoreBorneMan = $v['restore_total'] * $v['restore_rate'] / 100;
36− $tenantTotalMan = $restoreBorneMan + $v['cleaning'] + $v['unpaid'] + $v['other'];
37− $refundMan = $v['deposit'] - $tenantTotalMan;
58+ $restoreBorne = intdiv(2 * $restoreTotal * $rate + 10000, 20000);
59+ $tenantTotal = $restoreBorne + $cleaning + $unpaid + $other;
3860 return [
39− 'restore_borne' => (int)round($restoreBorneMan * 10000),
40− 'tenant_total' => (int)round($tenantTotalMan * 10000),
41− 'refund' => (int)round($refundMan * 10000),
61+ 'restore_borne' => $restoreBorne,
62+ 'tenant_total' => $tenantTotal,
63+ 'refund' => $deposit - $tenantTotal,
4264 ];
4365 }

仕様

着手前に測ったこと(2026-11-10)

元ツールの式(万円の2進数の小数・項目ごとに Math.round(万円 × 10000))を、本 spec の定義と比べた。原状回復費 100〜300,000円(100円刻み・3,000通り)× 負担割合25通り(0〜100% の5%刻み21通りと 12.5・37.5・62.5・87.5%)× 敷金5通り(0・5・10・20・30万円)× クリーニング2通り(0・3万円)の750,000通り。

負担割合答えが違った組み合わせ
5%刻み630,000通り中0通り
12.5・37.5・62.5・87.5%120,000通り中51,205通り

違ったのは、借主負担の原状回復費がちょうど 0.5円の位置に来る入力。

  • 返金額を 敷金 − 負担 の小数から別に丸めるので、0.5円の位置では返金が1円多くなり、負担と返金を足すと敷金より1円多い(120,000通り中51,188通り)。例: 敷金10万円・原状回復 5.25万円 × 12.5% → 負担 6,563円・返金 93,438円(正しくは 93,437円)
  • 万円の小数のまま掛けると 0.5円が 0.499… になって、負担分そのものが1円少なくなる(4,250通り)。例: 原状回復 8.29万円 × 12.5% = 10,362.5円が 10,362円になる(正しくは 10,363円)
  • 敷金で足りないときは、マイナスの 0.5 を Math.round が大きい方(0 に近い方)へ丸めるので、足りない額が1円少なくなる。例: 敷金0円・原状回復 5.15万円 × 12.5% → 返金 -6,437円(正しくは -6,438円)

本 spec の35件のうち、答えのある24件では、元ツールの式は13件で一致した。

入力

すべて文字列。

項目形説明
deposit金額預けた敷金(万円)
restore_total金額原状回復費の総額(万円)。通常損耗・経年劣化を除いた額
restore_rate割合原状回復費の借主負担割合(%)
cleaning金額ハウスクリーニング費(万円)
unpaid金額未払いの賃料など(万円)
other金額その他の借主負担(万円)
  • 金額は [0-9]+(\.[0-9]{1,4})?(小数第4位まで=1円単位)で、0〜10000(1億円)
  • 割合は [0-9]+(\.[0-9]{1,2})?(小数第2位まで)で、0〜100

計算

正確な値で計算する(万円の小数を2進数の小数にしない)。

  1. 原状回復費の借主負担分(円)= 原状回復費の総額 × 10000 × 負担割合 ÷ 100 を、1円未満を四捨五入(ちょうど半分は大きい方へ)
  2. 借主負担の合計(円)= 1 + クリーニング + 未払い + その他(どれも × 10000 で円の整数になる)
  3. 返金(円)= 敷金 × 10000 − 2。丸めない(1と2が円の整数なので、そのまま引く)。マイナスなら敷金では足りず、その額を追加で払う

丸めるのは1の1回だけ。だから内訳を足すと負担の合計になり、負担の合計と返金を足すと敷金になる。

出力

成功したとき(すべて整数・円): {"restore_borne": 40000, "tenant_total": 90000, "refund": 110000}

項目定義
restore_borne原状回復費の借主負担分
tenant_total借主負担の合計
refund返ってくる敷金。マイナスは足りない額

判定できないとき: {"error": "invalid_input"}(どれかの項目が上の形でない、または範囲の外)

元ツールとの違い

  • 元ツールは数の形をした入力なら何でも受け付け(1e3 も読む)、敷金が負なら計算せず、ほかの負の金額は 0 に、負担割合は 0〜100% に寄せる。本 spec は形と範囲の外を invalid_input にした
  • 元ツールは項目ごとに 万円 × 10000 を丸める。本 spec は上の「計算」のとおり1回だけ丸める
  • 実際の精算は、経過年数による負担割合の減り方(クロスは6年で残存価値1円など)や特約で変わる。本 spec は元ツールと同じく、負担割合を入力で受け取る目安