uniapp 金额处理组件

html 复制代码
<template>
	<view :class="{ line: line, weight: weight }" :style="{ color: color, fontFamily: SemiBold ? 'SemiBold' : 'Regular' }" class="base-money">
		<text v-show="preFix" class="preFix" :style="{ 'font-size': preFixSize + 'rpx', color: textColor }">{{ preFix }}</text>
		<text class="symbol" :style="{ 'font-size': symbolSize + 'rpx' }">¥</text>
		<text class="integer" :style="{ 'font-size': integerSize + 'rpx' }">{{ integer }}</text>
		<text v-if="digits && showDigits" class="decimal" :style="{ 'font-size': decimalSize + 'rpx' }">.{{ decimal }}</text>
	</view>
</template>

<script>
export default {
	name: 'BaseMoney',
	props: {
		// 小数位数,为0则不显示
		digits: {
			type: Number,
			default: 2
		},
		money: {
			type: String | Number,
			default: ''
		},
		// 删除线
		line: {
			type: Boolean,
			default: false
		},
		// 粗体
		weight: {
			type: Boolean,
			default: false
		},
		color: {
			type: String,
			default: '#f50'
		},
		textColor: {
			type: String,
			default: '#999'
		},
		symbolSize: {
			type: String,
			default: '20'
		},
		integerSize: {
			type: String,
			default: '26'
		},
		decimalSize: {
			type: String,
			default: '24'
		},
		inline: {
			type: Boolean,
			default: false
		},
		preFix: {
			type: String,
			default: ''
		},
		preFixSize: {
			type: String,
			default: '24'
		},
		SemiBold: {
			type: Boolean,
			default: true
		},
		isCoupon: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			integer: 0,
			decimal: 0,
			showDigits: false
		};
	},
	watch: {
		money: {
			handler(newValue, oldValue) {
				let value = Number(newValue).toFixed(this.digits);
				value = value.split('.');
				this.integer = value[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
				if (value[1]) {
					this.decimal = value[1];
					if ((this.isCoupon && this.decimal != '00') || !this.isCoupon) {
						this.showDigits = true;
					}
				}
			},
			immediate: true
		}
	}
};
</script>

<style lang="scss" scoped>
.base-money {
	display: inline-block;
	&.line {
		text-decoration: line-through;
	}

	&.weight {
		font-weight: 500;
	}
}
.preFix {
	font-weight: 500 !important;
	font-family: PingFang SC-Medium, PingFang SC !important;
}
.SemiBold {
	font-family: 'SemiBold';
}
.Regular {
	font-family: 'Regular';
}
</style>

效果展示

相关推荐
coder_pig32 分钟前
跟🤡杰哥一起学Flutter (三十四、玩转Flutter手势✋)
前端·flutter·harmonyos
万少38 分钟前
01-自然壁纸实战教程-免费开放啦
前端
独立开阀者_FwtCoder40 分钟前
【Augment】 Augment技巧之 Rewrite Prompt(重写提示) 有神奇的魔法
前端·javascript·github
yuki_uix1 小时前
AI辅助网页设计:从图片到代码的实践探索
前端
我想说一句1 小时前
事件机制与委托:从冒泡捕获到高效编程的奇妙之旅
前端·javascript
陈随易1 小时前
MoonBit助力前端开发,加密&性能两不误,斐波那契测试提高3-4倍
前端·后端·程序员
汤姆Tom1 小时前
JavaScript reduce()函数详解
javascript
小飞悟1 小时前
你以为 React 的事件很简单?错了,它暗藏玄机!
前端·javascript·面试
中微子1 小时前
JavaScript 事件机制:捕获、冒泡与事件委托详解
前端·javascript
Whoisshutiao1 小时前
网安-XSS-pikachu
前端·安全·网络安全