n2words
Version:
Convert numbers to words in 70+ languages with zero dependencies. Supports BigInt, decimals, and browser/Node.js environments.
3 lines (2 loc) • 5.1 kB
JavaScript
/*! n2words/ja-JP v5.1.2 | MIT License | github.com/forzagreen/n2words */
function e(e){let[n,t]=e.toLowerCase().split("e");const r=parseInt(t,10);let i="";"-"===n[0]&&(i="-",n=n.slice(1));const o=n.indexOf("."),s=-1===o?n:n.slice(0,o)+n.slice(o+1),u=(-1===o?n.length:o)+r;return u>=s.length?i+s+"0".repeat(u-s.length):u<=0?i+"0."+"0".repeat(-u)+s:i+s.slice(0,u)+"."+s.slice(u)}function n(n){const r=n.toString();return t(r)?e(r):r}function t(e){return e.includes("e")||e.includes("E")}function r(e){const n="-"===e[0];n&&(e=e.slice(1));const t=e.indexOf(".");if(-1===t)return{isNegative:n,integerPart:BigInt(e)};const r=e.slice(0,t)||"0",i=e.slice(t+1);return{isNegative:n,integerPart:BigInt(r),decimalPart:i}}function i(n){let r=n.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid currency format: "${n}"`);t(r)&&(r=e(r));const i="-"===r[0];i&&(r=r.slice(1));const o=r.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(r),cents:0n};const s=r.slice(0,o)||"0",u=(r.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function o(e,n,t){if(null===n)return;if(e<n)return;const r=n.toString().length-1,i=n===10n**BigInt(r)?`10^${r} - 1`:""+(n-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${i}`)}function s(e){return 10n**BigInt(4*(e+1))}const u=["","一","二","三","四","五","六","七","八","九"],a=["万","億","兆","京","垓","秭","穣","溝","澗","正","載","極","恒河沙","阿僧祇","那由他","不可思議","無量大数"],c=s(a.length),g=s(a.length),l=s(a.length),f="マイナス";function m(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10)%10,r=Math.trunc(e/100)%10,i=Math.trunc(e/1e3);let o="";return i>0&&(o+=1===i?"千":u[i]+"千"),r>0&&(o+=1===r?"百":u[r]+"百"),t>0&&(o+=1===t?"十":u[t]+"十"),n>0&&(o+=u[n]),o}function b(e){if(0n===e)return"零";if(e<10000n)return m(Number(e));if(e<100000000n){const n=Number(e/10000n),t=Number(e%10000n);let r;return r=1===n?"一"+a[0]:m(n)+a[0],t>0&&(r+=m(t)),r}return function(e){const n=[];let t=e;for(;t>0n;)n.push(Number(t%10000n)),t/=10000n;let r="";for(let e=n.length-1;e>=0;e--){const t=n[e];0!==t&&(r+=e>0?1===t?"一"+a[e-1]:m(t)+a[e-1]:m(t))}return r||"零"}(e)}function w(i){const{isNegative:s,integerPart:a,decimalPart:g}=function(i){const o=typeof i;if("bigint"==typeof i)return i<0n?{isNegative:!0,integerPart:-i}:{isNegative:!1,integerPart:i};if("number"==typeof i){if(!Number.isFinite(i))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(i)?i<0?{isNegative:!0,integerPart:BigInt(-i)}:{isNegative:!1,integerPart:BigInt(i)}:r(n(i))}if("string"==typeof i)return r(function(n){const r=n.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid number format: "${n}"`);return t(r)?e(r):r}(i));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(i);o(a,c);let l="";return s&&(l=f),l+=b(a),g&&(l+="点"+function(e){let n="";for(let t=0;t<e.length;t++){const r=parseInt(e[t],10);n+=0===r?"零":u[r]}return n}(g)),l}function d(n){const r=function(n){const r=typeof n;if("bigint"==typeof n){if(n<=0n)throw new RangeError("Ordinals must be positive integers");return n}if("number"==typeof n){if(!Number.isFinite(n))throw new RangeError("Ordinals must be finite numbers");if(!Number.isInteger(n))throw new RangeError("Ordinals must be whole numbers");if(n<=0)throw new RangeError("Ordinals must be positive integers");return BigInt(n)}if("string"==typeof n)return function(n){const r=n.trim();if(0===r.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===r[0])throw new RangeError("Ordinals cannot be negative");if(r.includes("."))throw new RangeError("Ordinals must be whole numbers");if(t(r)){const n=e(r);if(n.includes("."))throw new RangeError("Ordinals must be whole numbers");const t=BigInt(n);if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}try{const e=BigInt(r);if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}catch(e){if(e instanceof RangeError)throw e;throw new RangeError(`Invalid ordinal format: "${n}"`,{cause:e})}}(n);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${r}`)}(n);return o(r,g),"第"+b(r)}function h(e){const{isNegative:t,dollars:r,cents:s}=function(e){const t=typeof e;if("bigint"==typeof e)return e<0n?{isNegative:!0,dollars:-e,cents:0n}:{isNegative:!1,dollars:e,cents:0n};if("number"==typeof e){if(!Number.isFinite(e))throw new RangeError("Currency must be a finite number");return Number.isSafeInteger(e)?e<0?{isNegative:!0,dollars:BigInt(-e),cents:0n}:{isNegative:!1,dollars:BigInt(e),cents:0n}:i(n(e))}if("string"==typeof e)return i(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);o(r,l);let u="";return t&&(u=f),r>0n&&(u+=b(r)+"円"),s>0n&&(u+=b(s)+"銭"),0n===r&&0n===s&&(u+="零円"),u}export{c as cardinalMax,l as currencyMax,g as ordinalMax,w as toCardinal,h as toCurrency,d as toOrdinal};