将数字格式化为带printf()的货币

问题描述 投票:-1回答:1

如何使用printf()函数将字符串格式化为欧洲货币?在谷歌应用程序脚本中,基于生成pdf的电子表格,使用此代码,我的数值为4876.00我怎么能得到像4.876,00这样的数值呢?

printf()可以解决吗?完全谢谢我对javascript不太熟悉

(代码)

function testNumber() {
  var value = 1234.56789
  var a = format(value, {numberOfDecimalPlaces: 2})
  debugger
}

function testObject() {
  var value = {a: 1, b: 2}
  var a = format(value)
  debugger
}

function formatString(value, options) {

  var formattedValue = value
  options = options || {}

  if (typeof value === 'number') {

    var placeholder = '%s'
    var numberOfDecimalPlaces = 2

    if (options.numberOfDecimalPlaces !== undefined) {
      numberOfDecimalPlaces = options.numberOfDecimalPlaces 
    }

    if (numberOfDecimalPlaces > 0) {
      placeholder = '%.' + numberOfDecimalPlaces + 'f'
    }

    var multiplier = Math.pow(10, numberOfDecimalPlaces)

    formattedValue = Utilities.formatString(placeholder, Math.round(value * multiplier) / multiplier)

  }

    formattedValue = Utilities.formatDate(value, timeZone, dateFormat)

  } else if (typeof value === 'object') {

    formattedValue = JSON.stringify(value)
  }

  return formattedValue
}
google-apps-script formatting printf currency
1个回答
0
投票

如果您知道如何使用printf(),请尝试使用:Utilities.formatString()

© www.soinside.com 2019 - 2024. All rights reserved.