Ngay bây giờ tôi đang lưu trữ các số dưới dạng gấp đôi [11,3] trong cơ sở dữ liệu của mình. Một số con số có thể là tiêu cực lớn hơn. Cái tôi đang chơi ngay bây giờ là -3000.000
Khi tôi sử dụng number_format chỉ trên 3000.000, nó sẽ trả về 3.000 khi tôi sử dụng number_format trên -3000.000 nó trả về -3
Bất kỳ ý tưởng tại sao điều này đang xảy ra và những gì tôi có thể làm để sửa chữa nó? Tôi không phải là một sự mất mát ngay bây giờ về lý do tại sao điều này xảy ra.
Thanks,
Jeff
Chỉnh sửa: Tôi đã làm việc với mã sau: I got it to work with the following code:
$number = abs[$row['Amount']] * -1; $final = number_format[$number,2];
Bây giờ tại sao điều đó sẽ hoạt động và không:
$final = number_format[$row['Amount'],2];
Tôi không có manh mối, nhưng ít nhất tôi đã tìm thấy một giải pháp, cảm ơn vì sự giúp đỡ :]
Lio ¶ 4 năm trước Theo quan điểm của tôi, có một tác dụng phụ kém khi sử dụng các số âm sẽ tròn đến 0 [không]. E. G.:$a = -0.00000003; $ x = number_format [$ a, 2]; // -> -0.00 $ x = number_format [$ a, 0]; // -> -0 Bạn thấy dấu trừ ở phía trước đầu ra? Điều này gây khó chịu đặc biệt khi nó xảy ra vì sự không chính xác của các hoạt động điểm nổi bên trong PHP. E. G.:$a = 7,8; $ b = 7,79; $ c = 0,01; $ result = $ a - $ b - $ c; $ x = number_format [$ result, 2]; // -> -0.00 Trong trường hợp này, dấu trừ [-0.00] là kết quả của thực tế, kết quả là -0.000000000000021337 do không chính xác điểm nổi. Tuy nhiên, kết quả thay đổi do cài đặt của Php.ini và phiên bản PHP. Trong các thử nghiệm của tôi, tôi đã sử dụng Php 5.3 với độ chính xác của điểm nổi là 13 chữ số thập phân. Bạn có thể tránh hành vi này của number_format [] bằng cách làm tròn giá trị vào các vị trí thập phân cần thiết ngay trước khi chuyển nó đến hàm. E. G.:$a = -0.00000003; $ x = number_format [vòng [$ a, 2], 2]; // -> 0,00 $ x = number_format [vòng [$ a, 0], 0]; // -> 017 năm trước
$a = 2.658;
$x = number_format[$a, 2]; // --> 2.66
$x = number_format[$a, 0]; // --> 3
$a = -0.00000003;
$x = number_format[$a, 2]; // -->
-0.00
$x = number_format[$a, 0]; // --> -0
$a = 7.8;
$b = 7.79;
$c = 0.01;
$result = $a - $b - $c;
$x = number_format[$result, 2]; // --> -0.00
$a = -0.00000003;
$x =
number_format[round[$a, 2], 2]; // --> 0.00
$x = number_format[round[$a, 0], 0]; // --> 0
Bài viết đề xuất
1 bình luận
Bình luận
Marc dot vanwoerkom tại Fernuni-Hagen Dot de ¶
Ali Okan Yksel ¶
12 năm trước
Samuelpeixoto tại Gmail Dot Com ¶ — Format a number with grouped thousands
13 năm trước
Woodynadobhar tại Hotmail Dot Com ¶[
float $num
,
int $decimals
= 0,
?string
$decimal_separator
= ".",
?string $thousands_separator
= ","
]: string
Dipu dot ashok dot 17 tại gmail dot com ¶
[Php 4, Php 5, Php 7, Php 8]number_format - định dạng một số có hàng ngàn
Sự mô tảnumber_format [& nbsp; & nbsp; & nbsp; & nbsp; float ________ 2, & nbsp; & nbsp; & nbsp; & nbsp; ? Chuỗi $thousands_separator
= ","]: Chuỗi
num
decimals
Đặt số chữ số thập phân. Nếu 0
, decimal_separator
bị bỏ qua từ giá trị trả về.
decimal_separator
Đặt dấu phân cho điểm thập phân.
8.0.0 | Woodynadobhar tại Hotmail Dot Com ¶number_format[] accepted one, two, or four parameters [but not three]. |
7.2.0 | Dipu dot ashok dot 17 tại gmail dot com ¶ was changed to not being able to return 3, previously 3 could be returned for cases like where num would be 6.
|
[Php 4, Php 5, Php 7, Php 8]
number_format - định dạng một số có hàng ngànnumber_format[] Example
Sự mô tả
$final = number_format[$row['Amount'],2];
7$final = number_format[$row['Amount'],2];
8$final = number_format[$row['Amount'],2];
9number_format [& nbsp; & nbsp; & nbsp; & nbsp; float ________ 2, & nbsp; & nbsp; & nbsp; & nbsp; ? Chuỗi $thousands_separator
= ","]: Chuỗi
- Thông số
-
num
- Số được định dạng.
-
decimals
Đặt số chữ số thập phân. Nếu 0
, decimal_separator
bị bỏ qua từ giá trị trả về. ¶
Samuelpeixoto tại Gmail Dot Com ¶
$num
0
$num
1
$num
213 năm trước ¶
Woodynadobhar tại Hotmail Dot Com ¶
$num
3
Dipu dot ashok dot 17 tại gmail dot com ¶ ¶
Samuelpeixoto tại Gmail Dot Com ¶
$num
4
$num
5
$num
6
$num
713 năm trước ¶
Woodynadobhar tại Hotmail Dot Com ¶
$num
8
$num
9
$decimals
0
$decimals
1
$decimals
2
$decimals
3
$num
2Dipu dot ashok dot 17 tại gmail dot com ¶ ¶
17 năm trước
$decimals
5
$decimals
6
$decimals
7
$num
2[Php 4, Php 5, Php 7, Php 8] ¶
number_format - định dạng một số có hàng ngàn
$decimals
9
$decimal_separator
0
$num
2Sự mô tả ¶
Samuelpeixoto tại Gmail Dot Com ¶
$decimal_separator
2
$decimal_separator
3
$decimal_separator
4
$decimal_separator
5
$decimal_separator
613 năm trước ¶
Woodynadobhar tại Hotmail Dot Com ¶
$decimal_separator
7
$decimal_separator
8
$decimal_separator
9
$num
2Dipu dot ashok dot 17 tại gmail dot com ¶ ¶
[Php 4, Php 5, Php 7, Php 8]
$thousands_separator
1
$thousands_separator
2
$thousands_separator
3number_format - định dạng một số có hàng ngàn ¶
Sự mô tả
$thousands_separator
4
$thousands_separator
5
$thousands_separator
6
$thousands_separator
7
$num
2 number_format [& nbsp; & nbsp; & nbsp; & nbsp; float ________ 2, & nbsp; & nbsp; & nbsp; & nbsp; ? Chuỗi $thousands_separator
= ","]: Chuỗi ¶
17 năm trước
$thousands_separator
9
num
0
$num
2Thông số ¶
Woodynadobhar tại Hotmail Dot Com ¶
num
2
num
3
num
4
$num
2Dipu dot ashok dot 17 tại gmail dot com ¶ ¶
[Php 4, Php 5, Php 7, Php 8]
num
6
num
7
num
8
num
9number_format - định dạng một số có hàng ngàn ¶
Sự mô tả
decimals
0
decimals
1
decimals
2
decimals
3
decimals
4
decimals
5
decimals
6
decimals
7
decimals
8
decimals
9
0
0
0
1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
$num
2 number_format [& nbsp; & nbsp; & nbsp; & nbsp; float ________ 2, & nbsp; & nbsp; & nbsp; & nbsp; ? Chuỗi $thousands_separator
= ","]: Chuỗi ¶
Thông số
decimal_separator
1
decimal_separator
2
$num
2
num
¶
Số được định dạng.
decimal_separator
4
decimals
¶
Đặt số chữ số thập phân. Nếu 0
, decimal_separator
bị bỏ qua từ giá trị trả về.
decimal_separator
5
decimal_separator
6
decimal_separator
7
decimal_separator
8
decimal_separator
9
$final = number_format[$row['Amount'],2];
00$final = number_format[$row['Amount'],2];
01$final = number_format[$row['Amount'],2];
02$num
2 decimal_separator
¶
Woodynadobhar tại Hotmail Dot Com ¶
$final = number_format[$row['Amount'],2];
04$final = number_format[$row['Amount'],2];
05$final = number_format[$row['Amount'],2];
06$final = number_format[$row['Amount'],2];
0713 năm trước ¶
Woodynadobhar tại Hotmail Dot Com ¶
$final = number_format[$row['Amount'],2];
08Dipu dot ashok dot 17 tại gmail dot com ¶ ¶
Samuelpeixoto tại Gmail Dot Com ¶
$final = number_format[$row['Amount'],2];
09$final = number_format[$row['Amount'],2];
10$final = number_format[$row['Amount'],2];
11$final = number_format[$row['Amount'],2];
12$final = number_format[$row['Amount'],2];
13$final = number_format[$row['Amount'],2];
14[Php 4, Php 5, Php 7, Php 8] ¶
number_format - định dạng một số có hàng ngàn
$final = number_format[$row['Amount'],2];
15$final = number_format[$row['Amount'],2];
16$final = number_format[$row['Amount'],2];
17$final = number_format[$row['Amount'],2];
18$num
2Sự mô tả ¶
number_format - định dạng một số có hàng ngàn
$final = number_format[$row['Amount'],2];
20$final = number_format[$row['Amount'],2];
21$final = number_format[$row['Amount'],2];
22$final = number_format[$row['Amount'],2];
23$num
2Sự mô tả ¶
number_format [& nbsp; & nbsp; & nbsp; & nbsp; float ________ 2, & nbsp; & nbsp; & nbsp; & nbsp; ? Chuỗi $thousands_separator
= ","]: Chuỗi
$final = number_format[$row['Amount'],2];
25$final = number_format[$row['Amount'],2];
26$num
2Thông số ¶
num
$final = number_format[$row['Amount'],2];
28$final = number_format[$row['Amount'],2];
29$final = number_format[$row['Amount'],2];
30$final = number_format[$row['Amount'],2];
31$num
2Số được định dạng. ¶
[Php 4, Php 5, Php 7, Php 8]
$final = number_format[$row['Amount'],2];
33$final = number_format[$row['Amount'],2];
34$final = number_format[$row['Amount'],2];
35$num
2number_format - định dạng một số có hàng ngàn ¶
Samuelpeixoto tại Gmail Dot Com ¶
$final = number_format[$row['Amount'],2];
37$final = number_format[$row['Amount'],2];
38$final = number_format[$row['Amount'],2];
39$num
2Sự mô tả ¶
Samuelpeixoto tại Gmail Dot Com ¶
$final = number_format[$row['Amount'],2];
41$final = number_format[$row['Amount'],2];
42$final = number_format[$row['Amount'],2];
43$final = number_format[$row['Amount'],2];
44$num
2 number_format [& nbsp; & nbsp; & nbsp; & nbsp; float ________ 2, & nbsp; & nbsp; & nbsp; & nbsp; ? Chuỗi $thousands_separator
= ","]: Chuỗi ¶
Sự mô tả
$final = number_format[$row['Amount'],2];
46$final = number_format[$row['Amount'],2];
47$num
2 number_format [& nbsp; & nbsp; & nbsp; & nbsp; float ________ 2, & nbsp; & nbsp; & nbsp; & nbsp; ? Chuỗi $thousands_separator
= ","]: Chuỗi ¶
Thông số
$final = number_format[$row['Amount'],2];
49$final = number_format[$row['Amount'],2];
50$num
2 num
¶
Thông số
$final = number_format[$row['Amount'],2];
52$final = number_format[$row['Amount'],2];
53$num
2Jeroen de Bruijn [NL] ¶
17 năm trước
$final = number_format[$row['Amount'],2];
55$final = number_format[$row['Amount'],2];
56$num
2Marc dot vanwoerkom tại Fernuni-Hagen Dot de ¶ ¶
17 năm trước
$final = number_format[$row['Amount'],2];
58$final = number_format[$row['Amount'],2];
59$num
2Marc dot vanwoerkom tại Fernuni-Hagen Dot de ¶ ¶
12 năm trước
$final = number_format[$row['Amount'],2];
61$final = number_format[$row['Amount'],2];
62$final = number_format[$row['Amount'],2];
63$final = number_format[$row['Amount'],2];
64Samuelpeixoto tại Gmail Dot Com ¶ ¶
13 năm trước
$final = number_format[$row['Amount'],2];
65$final = number_format[$row['Amount'],2];
66$final = number_format[$row['Amount'],2];
67$num
2Woodynadobhar tại Hotmail Dot Com ¶ ¶
17 năm trước
$final = number_format[$row['Amount'],2];
69$final = number_format[$row['Amount'],2];
70$num
2Dipu dot ashok dot 17 tại gmail dot com ¶ ¶
12 năm trước
$final = number_format[$row['Amount'],2];
72$final = number_format[$row['Amount'],2];
73$num
2