簡體   English   中英

如何使用2位小數而不是使用javascript獲取數字?

[英]How to Get number with 2 decimal and not rounding using javascript?

如何使用2位小數而不是使用javascript獲取數字?

我嘗試

http://jsfiddle.net/3AaAx/31/

var i = "1234.666";
var xxx = Math.floor(i * 100) / 100
alert(xxx);

http://jsfiddle.net/3AaAx/29/

var i = "1234.666";

function myToFixed(i, digits) {
    var pow = Math.pow(10, digits);

    return Math.floor(i * pow) / pow;
}
var xxx = myToFixed(i, 2)
alert(xxx);

是工作。

但是當我聲明var時, i = "1234"; 我想得到"1234.00"

http://jsfiddle.net/3AaAx/30/

http://jsfiddle.net/3AaAx/32/

怎么做 ?

不舍入是一個不尋常的要求,這太糟糕了,因為如果可以舍入,則可以使用toFixed

var num = 1234.5678;
snippet.log(num.toFixed(2)); // "1234.57" -- note the rounding!

 var num = 1234.5678; snippet.log(num.toFixed(2)); // "1234.57" -- note the rounding! 
 <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

(不,您不能只執行.toFixed(3)然后砍掉最后一位數字;舍入並不一定只改變最后一位數字,考慮在1111.9999上使用toFixed(3) ,這會給您1112.000 。最后一位數字仍然可以為您提供四舍五入的結果。)

要不進行四舍五入,我認為沒有很多可用但蠻力的:

// Assumes `digits` is never more than 20
function toFixedNoRounding(num, digits) {
  var parts = String(num).split('.');
  if (digits <= 0) {
    return parts[0];
  }
  var fractional = (parts[1] || "0") + "000000000000000000000";
  return parts[0] + "." + fractional.substring(0, digits);
}

 // Assumes `digits` is never more than 20 function toFixedNoRounding(num, digits) { var parts = String(num).split('.'); if (digits <= 0) { return parts[0]; } var fractional = (parts[1] || "0") + "000000000000000000000"; return parts[0] + "." + fractional.substring(0, digits); } var num, notRounded; num = 1234; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); num = 1234.5678; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); num = 1234.1; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); num = 1234.999999; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); 
 <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

或者,您可以使用indexOf ,但似乎更笨拙:

// Assumes `digits` is never more than 20
function toFixedNoRounding(num, digits) {
  var str = String(num);
  var n = str.indexOf(".") + 1;
  if (n === 0) {
    str += ".";
    n = str.length;
  }
  n = digits <= 0 ? n - 1 : n + digits;
  if (str.length !== n) {
    str = (str + "00000000000000000000").substring(0, n);
  }
  return str;
}

 // Assumes `digits` is never more than 20 function toFixedNoRounding(num, digits) { var str = String(num); var n = str.indexOf(".") + 1; if (n === 0) { str += "."; n = str.length; } n = digits <= 0 ? n - 1 : n + digits; if (str.length !== n) { str = (str + "00000000000000000000").substring(0, n); } return str; } var num, notRounded; num = 1234; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); num = 1234.5678; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); num = 1234.1; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); num = 1234.999999; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); 
 <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

  var y = "1234.045";
  var s = number_with_2_decimal(y);
  alert(s);

  function number_with_2_decimal(a) {
  var x = Math.floor(a * 100) * 0.01;
  var str = String(x);
  var n = str.indexOf('.');
  if (n === -1) {
       str += '.00';
  };
  return str;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM