Hướng dẫn php number_format negative values - php number_format giá trị âm

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 đỡ :)

17 năm trước

Lio ¶

4 năm trước
$a = 2.658;
$x = number_format($a, 2); // --> 2.66
$x = number_format($a, 0); // --> 3

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
$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
$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); // -> 0
$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

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ề.

decimal_separator

Đặt dấu phân cho điểm thập phân.

$final = number_format($row['Amount'],2); 
1
13 năm trước
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
$final = number_format($row['Amount'],2); 
3, previously
$final = number_format($row['Amount'],2); 
3 could be returned for cases like where num would be
$final = number_format($row['Amount'],2); 
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); 
9

number_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 ¶

$num0

$num1

$num2

13 năm trước

Woodynadobhar tại Hotmail Dot Com ¶

$num3

Dipu dot ashok dot 17 tại gmail dot com ¶

Samuelpeixoto tại Gmail Dot Com ¶

$num4

$num5

$num6

$num7

13 năm trước

Woodynadobhar tại Hotmail Dot Com ¶

$num8

$num9

$decimals0

$decimals1

$decimals2

$decimals3

$num2

Dipu dot ashok dot 17 tại gmail dot com ¶

17 năm trước

$decimals5

$decimals6

$decimals7

$num2

(Php 4, Php 5, Php 7, Php 8)

number_format - định dạng một số có hàng ngàn

$decimals9

$decimal_separator0

$num2

Sự mô tả

Samuelpeixoto tại Gmail Dot Com ¶

$decimal_separator2

$decimal_separator3

$decimal_separator4

$decimal_separator5

$decimal_separator6

13 năm trước

Woodynadobhar tại Hotmail Dot Com ¶

$decimal_separator7

$decimal_separator8

$decimal_separator9

$num2

Dipu dot ashok dot 17 tại gmail dot com ¶

(Php 4, Php 5, Php 7, Php 8)

$thousands_separator1

$thousands_separator2

$thousands_separator3

number_format - định dạng một số có hàng ngàn

Sự mô tả

$thousands_separator4

$thousands_separator5

$thousands_separator6

$thousands_separator7

$num2

number_format (& nbsp; & nbsp; & nbsp; & nbsp; float ________ 2, & nbsp; & nbsp; & nbsp; & nbsp; ? Chuỗi $thousands_separator = ","): Chuỗi

17 năm trước

$thousands_separator9

num0

$num2

Thông số

Woodynadobhar tại Hotmail Dot Com ¶

num2

num3

num4

$num2

Dipu dot ashok dot 17 tại gmail dot com ¶

(Php 4, Php 5, Php 7, Php 8)

num6

num7

num8

num9

number_format - định dạng một số có hàng ngàn

Sự mô tả

decimals0

decimals1

decimals2

decimals3

decimals4

decimals5

decimals6

decimals7

decimals8

decimals9

00

01

02

03

04

05

06

07

08

09

$num2

number_format (& nbsp; & nbsp; & nbsp; & nbsp; float ________ 2, & nbsp; & nbsp; & nbsp; & nbsp; ? Chuỗi $thousands_separator = ","): Chuỗi

Thông số

decimal_separator1

decimal_separator2

$num2

num

Số được định dạng.

decimal_separator4

decimals

Đặt số chữ số thập phân. Nếu 0, decimal_separator bị bỏ qua từ giá trị trả về.

decimal_separator5

decimal_separator6

decimal_separator7

decimal_separator8

decimal_separator9

$final = number_format($row['Amount'],2); 
00

$final = number_format($row['Amount'],2); 
01

$final = number_format($row['Amount'],2); 
02

$num2

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); 
07

13 năm trước

Woodynadobhar tại Hotmail Dot Com ¶

$final = number_format($row['Amount'],2); 
08

Dipu 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

$num2

Sự 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

$num2

Sự 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

$num2

Thô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

$num2

Số đượ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

$num2

number_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

$num2

Sự 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

$num2

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

$num2

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

$num2

num

Thông số

$final = number_format($row['Amount'],2); 
52

$final = number_format($row['Amount'],2); 
53

$num2

Jeroen de Bruijn [NL]

17 năm trước

$final = number_format($row['Amount'],2); 
55

$final = number_format($row['Amount'],2); 
56

$num2

Marc 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

$num2

Marc 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); 
64

Samuelpeixoto 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

$num2

Woodynadobhar tại Hotmail Dot Com ¶

17 năm trước

$final = number_format($row['Amount'],2); 
69

$final = number_format($row['Amount'],2); 
70

$num2

Dipu 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

$num2