跳至內容
Astera Technology

比例匯入為何成 25,而非 0.25?

儲存格數值與百分比顯示分開處理,不把畫面數字當原值。

下載完整個案資料及參考答案 ↓

先核來源與已訂規則

本例數字格若明訂百分比格式,要取底層比例。0.25 顯成 25%,仍須存 0.25;已訂文字百分比則按其文字格式解。普通數字沒有這個表示依據便暫停。

已填假設輸入
紀錄輸入 JSON
原例輸入{"kind":"number","value":0.25,"style":"percent","display":"25%"}
下載可編輯 CSV ↓

比較兩條實際路徑

直接解析畫面 25% 會得 25,倍數已變。數字路徑照留 0.25;文字 25% 才明確計 25÷100。

錯誤與更正路徑的輸出
路徑輸出 JSON
錯誤路徑{"accepted":true,"proportion":25}
更正路徑{"accepted":true,"proportion":0.25}
下載可編輯 CSV ↓
本地 Node.js 程序:22.23.3 或 24.14.1
import {DatabaseSync} from 'node:sqlite';
import {TextDecoder} from 'node:util';
import {inflateSync} from 'node:zlib';
import {posix} from 'node:path';
import {createHash} from 'node:crypto';
const rows=(db,sql)=>db.prepare(sql).all().map(r=>({...r}));
const failed=(action)=>{try{action();return null;}catch(e){return {code:e.errcode??e.code??e.name};}};

import assert from 'node:assert/strict';
const run=function run(i,corrected=true){
    if(!corrected)return {accepted:true,proportion:parseFloat(i.display)};
    if(i.kind==='number'&&i.style==='percent')return {accepted:true,proportion:i.value};
    if(i.kind==='text'&&/^\d+(?:\.\d+)?%$/.test(i.value))return {accepted:true,proportion:Number(i.value.slice(0,-1))/100};
    return {accepted:false,error:'percentage-representation-not-declared'};
  };
const cases=[{"label":"misleading base","input":{"kind":"number","value":0.25,"style":"percent","display":"25%"},"expected":{"accepted":true,"proportion":25},"corrected":false},{"label":"corrected base","input":{"kind":"number","value":0.25,"style":"percent","display":"25%"},"expected":{"accepted":true,"proportion":0.25},"corrected":true},{"label":"declared percentage text","input":{"kind":"text","value":"25%","style":null,"display":"25%"},"expected":{"accepted":true,"proportion":0.25},"corrected":true},{"label":"number without declared percentage style","input":{"kind":"number","value":25,"style":null,"display":"25"},"expected":{"accepted":false,"error":"percentage-representation-not-declared"},"corrected":true}];
for(const test of cases){
  const actual=run(test.input,test.corrected);
  assert.deepEqual(actual,test.expected,test.label);
  console.log(test.label,actual);
}
下載可執行例子 ↓

下次載入前核邊界

已訂文字 25% 得 0.25;普通數字 25 未有百分比格式,不自行估比例。

可編輯邊界紀錄及預期輸出
情況輸入 JSON預期輸出 JSON
已訂文字百分比{"kind":"text","value":"25%","style":null,"display":"25%"}{"accepted":true,"proportion":0.25}
沒有百分比表示依據{"kind":"number","value":25,"style":null,"display":"25"}{"accepted":false,"error":"percentage-representation-not-declared"}
下載可編輯 CSV ↓

標準比例欄應填哪個值?

查看參考答案

0.25,顯示 25% 是該比例的呈現,不是另一個原數值 25。

相關資源