金额处理currency.js

发布时间 2023-05-28 14:04:35作者: 小白字太白
<template>
  <div>
    <el-button type="primary" @click="test">按钮</el-button>
    
    <p>

    </p>
  </div>
</template>

<script>
import currency from 'currency.js'
export default {
  data() {
    return {
      messge:"a",
    }
  },
  methods:{
    test() {
     
      // 数字转换

      // Numbers
      currency(1); // => "1.00"
      currency(123); // => "123.00"
      console.log(currency(123.11))
      console.log(currency(1222222223.11).value)
      console.log(currency("1222222223.11").value)

      // Decimals
      currency(1.00); // => "1.00"
      currency(1.23); // => "1.23"

      // Strings
      currency("1.23"); // => "1.23"
      currency("$12.30"); // => "12.30"
      currency("£1,234,567.89"); // => "1,234,567.89"

      // Currency
      let c1 = currency(1.23);
      let c2 = currency(4.56);
      currency(7.89).add(c1).add(c2); // => "13.68"

      // 金额加减
      2.51 + .01;                   // => 2.5199999999999996
      currency(2.51).add(.01);      // => 2.52
      console.log(currency(2.51).add(.01).value)
      console.log(currency(2.51).add(.01))
      2.52 - .01;                   // 2.5100000000000002
      currency(2.52).subtract(.01); // 2.51

      currency(123.45).multiply(2); // => "246.90"

      currency(123.45).divide(2); // => "61.73"

      //取值
      console.log(currency(123.45).add(0.01).value)

      //精确度 precision 

      currency(1.234, { precision: 2 }); // => "1.23"
      currency(1.234, { precision: 3 }); // => "1.234"


      // separator  分离器

      currency(1234.56, { separator: ',' }).format(); // => "1,234.56"
      currency(1234.56, { separator: ' ' }).format(); // => "1 234.56"

      console.log(currency(111222333444234.56, { separator: ',' }).format())
      console.log(currency(111222333444234.56, { separator: ',' }).format())

       //人民币转换
       const JPY = value => currency(value, { precision: 2, symbol: '¥' });

        console.log(JPY(1234).format()) //¥1,234.00
        console.log(JPY(1234.15).format()) //¥1,234.15

      

      //去掉货币符号
      const MONEY = value => currency(value, { precision: 2, symbol: '' });

      console.log(MONEY(1234).format()) //1,234.00
      console.log(MONEY(1112223334441234.15).format()) //1,234.15
      console.log(MONEY("1112223334441234.15").format()) //1,234.15


    }
  }
}
</script>

<style>

</style>