forked from CorentinTh/it-tools
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
翻译自述文件 + 新增人民币大写转换器 + 修复输出区域宽度溢出 (#3)
* 修复 format-transformer 输出区域宽度溢出 * 新增工具:人民币大写转换器 * 翻译自述文件 * -
- Loading branch information
Showing
7 changed files
with
165 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { CurrencyYen } from '@vicons/tabler'; | ||
import { defineTool } from '../tool'; | ||
|
||
export const tool = defineTool({ | ||
name: '人民币大写转换器', | ||
path: '/rmb-d', | ||
description: '人民币大写转换工具,提供在线人民币大写转换服务', | ||
keywords: ['人民币', '大写', '转换'], | ||
component: () => import('./rmb-d.vue'), | ||
icon: CurrencyYen, | ||
createdAt: new Date('2023-12-11'), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
export { rmb }; | ||
|
||
const numbers = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; | ||
const leftUnits = ['元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '万', '拾', '佰', '仟', '万']; | ||
const rightUnits = ['角', '分']; | ||
|
||
function rmb(value: number) { | ||
if (Object.prototype.toString.call(value) === '[object Number]' && value >= 0.01) { | ||
const fragment: any = []; | ||
const [leftValues, rightValues] = String(value).split('.').map(part => part.split('').map(i => Number(i))); | ||
|
||
const leftValueLength = leftValues.length; // 整数部分位数 | ||
const unit1 = leftValueLength - 1; // 元位 | ||
const unit5 = leftValueLength - 5; // 万位 | ||
const unit9 = leftValueLength - 9; // 亿位 | ||
const unit13 = leftValueLength - 13; // 万亿位 | ||
const unit17 = leftValueLength - 17; // 万万亿位 | ||
const hasLeftValue = leftValueLength > 1 || leftValues[0] > 0; // 整数部分不为0 | ||
const hasRightValue = rightValues && (rightValues[0] > 0 || rightValues[1] > 0); // 小数部分不为0 | ||
const has678Value = leftValues[unit5 - 1] > 0 || leftValues[unit5 - 2] > 0 || leftValues[unit5 - 3] > 0; // 拾万、佰万或仟万位不为0 | ||
const overflowIndex = leftValueLength - leftUnits.length; // 溢出位索引 | ||
|
||
let leftUnitIndex = 0; | ||
for (let i = leftValueLength - 1; i >= 0; i--) { | ||
if (leftValues[i] === 0 && (i === unit5 || i === unit9 || i === unit13 || i === unit17) && leftValues[i + 1] > 0) { | ||
// 当前位为0,且当前位为万、亿、万亿、万万亿,且低一位不为0 | ||
fragment.unshift({type:'number', value:numbers[leftValues[i]]}); | ||
} | ||
|
||
if ((leftValues[i] > 0) || (i === unit1 && hasLeftValue) || (i === unit5 && has678Value) || i === unit9 || i === unit13 || i === unit17) { | ||
// 元、万、亿、万亿、万万亿或当前位不为0 | ||
fragment.unshift({type:'unit', value:leftUnits[leftUnitIndex]}); | ||
} | ||
|
||
if (leftValues[i] > 0 || (leftValues[i + 1] > 0 && i !== unit5 && i !== unit9 && i !== unit13 && i !== unit17) || i <= overflowIndex) { | ||
// 当前位不为0,或低一位不为0且当前位非万、亿、万亿、万万亿,或当前为溢出位 | ||
fragment.unshift({type:'number', value:numbers[leftValues[i]]}); | ||
} | ||
|
||
leftUnitIndex++; | ||
} | ||
|
||
if (hasRightValue) { | ||
// 角 | ||
if (rightValues[0] > 0 || hasLeftValue) { // 角位不为0,或整数位不为0 | ||
fragment.push({type:'number', value:numbers[rightValues[0]]}); | ||
} | ||
if (rightValues[0] > 0) { // 角位不为0 | ||
fragment.push({type:'unit', value:rightUnits[0]}); | ||
} | ||
// 分 | ||
if (rightValues[1] > 0) { | ||
fragment.push({type:'number', value:numbers[rightValues[1]]}); | ||
fragment.push({type:'unit', value:rightUnits[1]}); | ||
} | ||
} else { // 没有小数位 | ||
fragment.push({type:'cut', value:'整'}); | ||
} | ||
return fragment; | ||
} | ||
return [ | ||
{type:'number', value:'零'}, | ||
{type:'unit', value:'元'}, | ||
{type:'cut', value:'整'}, | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<script setup lang="ts"> | ||
import { rmb } from './rmb-d.models'; | ||
import { useThemeVars } from 'naive-ui'; | ||
const themeVars = useThemeVars(); | ||
const inputRmb = ref(23); | ||
const outputRmb = computed(() => rmb(inputRmb.value));; | ||
</script> | ||
|
||
<template> | ||
<div flex flex-col gap-2> | ||
<c-card title="小写金额"> | ||
<n-input-number v-model:value="inputRmb" max="100000000000" min="0" placeholder="输入小写金额 (例:1314.52)" :show-button="false" w-full /> | ||
</c-card> | ||
|
||
<div my-16px divider /> | ||
|
||
<c-card title="大写金额" flex flex-col> | ||
<div m-0 m-x-auto> | ||
<span :class="item.type" | ||
v-for="(item, index) in outputRmb" | ||
:key="index" | ||
> | ||
{{ item.value }} | ||
</span> | ||
</div> | ||
</c-card> | ||
</div> | ||
</template> | ||
|
||
<style lang="less" scoped> | ||
.unit { | ||
font-size: 1.4em; | ||
color: v-bind('themeVars.successColor'); | ||
} | ||
.number { | ||
font-size: 2.4em; | ||
} | ||
.cut { | ||
font-size: 1.4em; | ||
color: v-bind('themeVars.errorColor'); | ||
} | ||
</style> |