Tôi muốn hiển thị số lượng lớn độc đáo hơn với dấu phẩy. Vì vậy, nếu số được nói 123456789, nó sẽ hiển thị 123.456.789. Tôi đã nhìn xung quanh nhưng tôi chỉ tìm thấy mã rằng sẽ không hoạt động theo cách tôi muốn vì vậy tôi đã hy vọng tôi có thể tìm thấy một số trợ giúp ở đây. Ngoài ra, tôi hy vọng nó cũng năng động, vì vậy dấu phẩy sẽ thay đổi khi số lượng thay đổi.
Số mà tôi muốn ảnh hưởng có ID = "Giá trị".
Đó là tất cả, tôi không nghĩ rằng tôi đang thiếu bất cứ điều gì. Vì vậy, một lần nữa tôi muốn số có ID = "Giá trị" đã được giới thiệu dấu phẩy khi cần thiết. Nếu bạn cần thêm thông tin xin vui lòng cho tôi biết!
Đã hỏi ngày 4 tháng 1 năm 2015 lúc 1:46Jan 4, 2015 at 1:46
2
Bạn có thể sử dụng toLocaleString
:
num.toLocaleString['en', {useGrouping:true}]
Đã trả lời ngày 4 tháng 1 năm 2015 lúc 1:51Jan 4, 2015 at 1:51
OrioloriolOriol
258K57 Huy hiệu vàng409 Huy hiệu bạc493 Huy hiệu Đồng57 gold badges409 silver badges493 bronze badges
1
Điều này đã được trả lời ở đây:
Cách in một số bằng dấu phẩy dưới dạng hàng ngàn người phân tách trong JavaScript
Trong trường hợp bạn không quan tâm đến việc đọc câu trả lời ở trên, mã được đưa ra là:
function numberWithCommas[x] {
return x.toString[].replace[/\B[?=[\d{3}]+[?!\d]]/g, ","];
}
Nếu bạn đang sử dụng jQuery, hãy sử dụng một cái gì đó như:
var val = parseInt[$['#value'].text[]];
//Use the code in the answer above to replace the commas.
val = numberWithCommas[val];
$['#value'].text[val];
Đã trả lời ngày 4 tháng 1 năm 2015 lúc 1:51Jan 4, 2015 at 1:51
6
Orioloriol
Number[x].toLocaleString[];
258K57 Huy hiệu vàng409 Huy hiệu bạc493 Huy hiệu Đồng
Điều này đã được trả lời ở đây:Apr 3, 2018 at 12:31
Cách in một số bằng dấu phẩy dưới dạng hàng ngàn người phân tách trong JavaScriptNelson
Trong trường hợp bạn không quan tâm đến việc đọc câu trả lời ở trên, mã được đưa ra là:16 silver badges21 bronze badges
1
Hàm này giả định những gì đang được gửi đến nó là một chuỗi, với một điểm thập phân và hai vị trí sau thập phân. Để có được số của bạn vào định dạng đó trước, hãy sử dụng điều này. Sau đó, chức năng này sẽ chính xác phân tách số. Ví dụ: 2345643.00 sẽ trả về 2.345.643.00function CommaFormatted[amount] {
var delimiter = ","; // replace comma if desired
var a = amount.split['.',2]
var d = a[1];
var i = parseInt[a[0]];
if[isNaN[i]] { return ''; }
var minus = '';
if[i < 0] { minus = '-'; }
i = Math.abs[i];
var n = new String[i];
var a = [];
while[n.length > 3] {
var nn = n.substr[n.length-3];
a.unshift[nn];
n = n.substr[0,n.length-3];
}
if[n.length > 0] { a.unshift[n]; }
n = a.join[delimiter];
if[d.length < 1] { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}
Giải pháp mẫu:-.
Mã HTML:
Mã JavaScript: hàm nghìn_separators [num] {var num_parts = num.toString []. Split ["." ....
Giải pháp mẫu:-.
Mã HTML:
Mã JavaScript: hàm nghìn_separators [num] {var num_parts = num.toString []. Split ["." ....
Trình bày bằng hình ảnh:.
- Bản thử trực tiếp: ... .Intl.NumberFormat[] object is used to represent numbers in a language-sensitive formatting. It can be used to represent currency or percentages according to the locale specified.
- Cải thiện giải pháp mẫu này và đăng mã của bạn thông qua Disqus ..
- Phương thức định dạng [] của đối tượng này có thể được sử dụng để trả về một chuỗi số của số trong các tùy chọn định dạng và locale được chỉ định. Điều này sẽ định dạng số với dấu phẩy tại hàng ngàn địa điểm và trả lại một chuỗi với số được định dạng.format[] method of this object can be used to return a string of the number in the specified locale and formatting options. This will format the number with commas at the thousands of places and return a string with the formatted number.
Syntax:
nfObject = new Intl.NumberFormat['en-US'] nfObject.format[givenNumber]
Example:
0] { a.unshift[n]; }
n = a.join[delimiter];
if[d.length < 1] { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}
8
function numberWithCommas[x] {
return x.toString[].replace[/\B[?=[\d{3}]+[?!\d]]/g, ","];
}
1nfObject = new Intl.NumberFormat['en-US'] nfObject.format[givenNumber]0
nfObject = new Intl.NumberFormat['en-US'] nfObject.format[givenNumber]1
nfObject = new Intl.NumberFormat['en-US'] nfObject.format[givenNumber]0
nfObject = new Intl.NumberFormat['en-US'] nfObject.format[givenNumber]3
function numberWithCommas[x] {
return x.toString[].replace[/\B[?=[\d{3}]+[?!\d]]/g, ","];
}
5var val = parseInt[$['#value'].text[]];
//Use the code in the answer above to replace the commas.
val = numberWithCommas[val];
$['#value'].text[val];
4function CommaFormatted[amount] {
var delimiter = ","; // replace comma if desired
var a = amount.split['.',2]
var d = a[1];
var i = parseInt[a[0]];
if[isNaN[i]] { return ''; }
var minus = '';
if[i < 0] { minus = '-'; }
i = Math.abs[i];
var n = new String[i];
var a = [];
while[n.length > 3] {
var nn = n.substr[n.length-3];
a.unshift[nn];
n = n.substr[0,n.length-3];
}
if[n.length > 0] { a.unshift[n]; }
n = a.join[delimiter];
if[d.length < 1] { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}
8function numberWithCommas[x] {
return x.toString[].replace[/\B[?=[\d{3}]+[?!\d]]/g, ","];
}
1Các
function CommaFormatted[amount] {
var delimiter = ","; // replace comma if desired
var a = amount.split['.',2]
var d = a[1];
var i = parseInt[a[0]];
if[isNaN[i]] { return ''; }
var minus = '';
if[i < 0] { minus = '-'; }
i = Math.abs[i];
var n = new String[i];
var a = [];
while[n.length > 3] {
var nn = n.substr[n.length-3];
a.unshift[nn];
n = n.substr[0,n.length-3];
}
if[n.length > 0] { a.unshift[n]; }
n = a.join[delimiter];
if[d.length < 1] { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}
2var val = parseInt[$['#value'].text[]];
//Use the code in the answer above to replace the commas.
val = numberWithCommas[val];
$['#value'].text[val];
4givenNumber.toLocaleString['en-US']0
function numberWithCommas[x] {
return x.toString[].replace[/\B[?=[\d{3}]+[?!\d]]/g, ","];
}
1function numberWithCommas[x] {
return x.toString[].replace[/\B[?=[\d{3}]+[?!\d]]/g, ","];
}
5