utils js
function fillNumberWithComma(num) {
num += "";
if (/^-?\d+(\.\d+)?$/.test(num)) {
//确认是数字
var segments = num.split(".");
return (
segments[0]
.split("")
.reverse()
.join("")
.replace(/(\d{3})/g, "$1,")
.split("")
.reverse()
.join("") + (segments[1] ? "." + segments[1] : "")
);
}
return false;
}
export { fillNumberWithComma };
引用
import { fillNumberWithComma } from "../utils/utils";
用法
data() {
return {
num : fillNumberWithComma(21234534)
}
}
呈现效果
- 21,234,534