Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数字格式化为1';000000.00如果超过一百万,则使用撇号_Javascript_Formatting_Currency - Fatal编程技术网

Javascript 将数字格式化为1';000000.00如果超过一百万,则使用撇号

Javascript 将数字格式化为1';000000.00如果超过一百万,则使用撇号,javascript,formatting,currency,Javascript,Formatting,Currency,我需要用撇号将金额格式化为百万,用逗号将金额格式化为千,用句号将金额格式化为小数。使用javascript。 最好的办法是什么?谢谢 一些例子: 987654321 -> 987'654,321.00 87654321 -> 87'654,321.00 7654321 -> 7'654,321.00 654321 -> 654,321.00 54321 -> 54,321.00 4321 ->

我需要用撇号将金额格式化为百万,用逗号将金额格式化为千,用句号将金额格式化为小数。使用javascript。 最好的办法是什么?谢谢

一些例子:

987654321 -> 987'654,321.00
 87654321 ->  87'654,321.00
  7654321 ->   7'654,321.00
   654321 ->     654,321.00
    54321 ->      54,321.00
     4321 ->       4,321.00
      321 ->         321.00
       21 ->          21.00
        1 ->           1.00

我想,这个功能可以帮助:

function formatNumber( num ) {
  // String with formatted number
  var totalStr = '';
  // Convert number to string
  var numStr = num + '';
  // Separate number on before point and after
  var parts = numStr.split( '.' );
  // Save length of rounded number
  var numLen = parts[0].length;
  // Start iterating numStr chars
  for ( var i = 0; i < numLen; i++ ) {
    // Position of digit from end of string
    var y = numLen - i;

    // If y is divided without remainder on 3...
    if ( i > 0 && y % 3 == 0 ) {
        // add aposrtoph when greater than 6 digit
        // or add point when smaller than 6 digit
        totalStr += y >= 6 ? '\'' : ',';
    }

    // Append current position digit to total string
    totalStr += parts[0].charAt(i);
  }
  // Return total formatted string with float part of number (or '.00' when haven't float part)
  return totalStr + '.' + ( parts[1] ? parts[1] : '00');
}
函数格式编号(num){
//带格式化数字的字符串
var totalStr='';
//将数字转换为字符串
var numStr=num+'';
//将点前和点后的数字分开
var parts=numStr.split('.');
//保存舍入数的长度
var numLen=零件[0]。长度;
//开始迭代numStr字符
对于(var i=0;i0&&y%3==0){
//大于6位时添加aposrtoph
//或在小于6位时添加点
totalStr+=y>=6?'\'':',';
}
//将当前位置数字追加到总字符串
totalStr+=零件[0]。字符(i);
}
//返回带浮点数部分的格式化字符串总数(如果没有浮点数部分,则返回“.00”)
返回totalStr+'。+(部分[1]?部分[1]:'00');
}

我想,这个函数可以帮助:

function formatNumber( num ) {
  // String with formatted number
  var totalStr = '';
  // Convert number to string
  var numStr = num + '';
  // Separate number on before point and after
  var parts = numStr.split( '.' );
  // Save length of rounded number
  var numLen = parts[0].length;
  // Start iterating numStr chars
  for ( var i = 0; i < numLen; i++ ) {
    // Position of digit from end of string
    var y = numLen - i;

    // If y is divided without remainder on 3...
    if ( i > 0 && y % 3 == 0 ) {
        // add aposrtoph when greater than 6 digit
        // or add point when smaller than 6 digit
        totalStr += y >= 6 ? '\'' : ',';
    }

    // Append current position digit to total string
    totalStr += parts[0].charAt(i);
  }
  // Return total formatted string with float part of number (or '.00' when haven't float part)
  return totalStr + '.' + ( parts[1] ? parts[1] : '00');
}
函数格式编号(num){
//带格式化数字的字符串
var totalStr='';
//将数字转换为字符串
var numStr=num+'';
//将点前和点后的数字分开
var parts=numStr.split('.');
//保存舍入数的长度
var numLen=零件[0]。长度;
//开始迭代numStr字符
对于(var i=0;i0&&y%3==0){
//大于6位时添加aposrtoph
//或在小于6位时添加点
totalStr+=y>=6?'\'':',';
}
//将当前位置数字追加到总字符串
totalStr+=零件[0]。字符(i);
}
//返回带浮点数部分的格式化字符串总数(如果没有浮点数部分,则返回“.00”)
返回totalStr+'。+(部分[1]?部分[1]:'00');
}