在outsystem内使用JavaScript封装一个FormatAmount方法

1,792 阅读1分钟

First 创建

在Interface栏下的Script目录新建一个Scrip文件,然就命名并编辑实现FormatAmount方法的代码

image.png

image.png

function formatAmount(amount, decimalDigits = 0) {
  const amountStr = String(Number(amount).toFixed(decimalDigits))
  // 使用正则表达式  
  const reg = /\B(?=(?:\d{3})+$)/g
  // 是否是小数
  const isDecimal = amountStr.indexOf('.') > -1
  if (isDecimal) {
     // 整数部分
     const integerPart = amountStr.substring(0, amountStr.indexOf('.'))
     // 小数部分
     const decimalPart = amountStr.substring(amountStr.length, amountStr.indexOf('.'))
     return `${integerPart.replace(reg, ',')}${decimalPart}`
  } else {
    return amountStr.replace(reg, ',')
  }
}

使用

在需要使用到的页面导入Js文件,如图所示

image.png

然后在action中便可以使用导入的FormatAmount方法,

image.png