格式化字符串欧元货币

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

在我的Google电子表格中,我应该在我的Apps脚本中可以使用以下格式的货币:xxx.xxx,xx

相反,以下脚本以xxxxxx.xx格式返回值该怎么做?

(这是我使用的代码)

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
}

/**
 * Format an object into a suitable value
 * 
 * @param {object} value
 * @param {object} options
 *   {number} numberOfDecimalPlaces [OPTIONAL, DEFAULT: 0]
 *   {string} dateFormat [OPTIONAL, DEFAULT: 'yyyy-MM-dd']
 *
 * @return {string} formattedValue
 */

function formatString(value, options) {

  var formattedValue = value
  options = options || {}

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

    var placeholder = '%s'
    var numberOfDecimalPlaces = 0

    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)

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

    formattedValue = JSON.stringify(value)
  }

  return formattedValue
}
google-apps-script google-sheets currency
2个回答
0
投票

使用Intl


0
投票

这是intl的最终代码,但不起作用。

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