Phương thức
# d, f and b are type
# integer
# float arguments
print[format[123.4567898, "f"]]
# binary format
3 trả về một biểu diễn được định dạng của giá trị đã cho được kiểm soát bởi trình xác định định dạngVí dụ
value = 45
# format the integer to binary
binary_value = format[value, 'b']
print[binary_value]
# Output: 101101
định dạng[] Cú pháp
Cú pháp của nó là
format[value[, format_spec]]
định dạng [] Tham số
Hàm
# d, f and b are type
# integer
# float arguments
print[format[123.4567898, "f"]]
# binary format
3 nhận hai tham số- giá trị - giá trị cần được định dạng
- format_spec - Thông số kỹ thuật về cách định dạng giá trị
Trình xác định định dạng có thể ở định dạng
[[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
Truy cập các liên kết này để tìm hiểu thêm về và
định dạng [] Giá trị trả về
Hàm
# d, f and b are type
# integer
# float arguments
print[format[123.4567898, "f"]]
# binary format
3 trả về một biểu diễn được định dạng của một giá trị đã cho được chỉ định bởi trình xác định định dạngví dụ 1. Định dạng số với format[]
# d, f and b are type
# integer
# float arguments
print[format[123.4567898, "f"]]
# binary format
đầu ra
123 123.456790 1100
ví dụ 2. Định dạng số với điền, căn chỉnh, ký hiệu, chiều rộng, độ chính xác và loại
# integer
print[format[1234, "*>+7,d"]]
# float number
print[format[123.4567, "^-09.3f"]]
đầu ra
*+1,234 0123.4570
Ở đây, khi định dạng số nguyên
format[value[, format_spec]]2, chúng tôi đã chỉ định định dạng định dạng
format[value[, format_spec]]3. Hãy hiểu từng tùy chọn
format[value[, format_spec]]
4 - Ký tự lấp đầy sẽ lấp đầy khoảng trống sau khi định dạngformat[value[, format_spec]]
5 - Đây là tùy chọn căn chỉnh bên phải giúp căn chỉnh chuỗi đầu ra sang bên phảiformat[value[, format_spec]]
6 - Tùy chọn ký hiệu buộc phải ký số [có ký hiệu ở bên trái]format[value[, format_spec]]
7 - Chính tùy chọn chiều rộng buộc số có chiều rộng tối thiểu là 7, các khoảng trắng khác sẽ được lấp đầy bởi ký tự điềnformat[value[, format_spec]]
8 - Toán tử hàng nghìn đặt dấu phẩy giữa tất cả các hàng nghìnformat[value[, format_spec]]
9 - Đây là tùy chọn loại chỉ định số là số nguyên
Khi định dạng số dấu phẩy động
[[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"0, chúng tôi đã chỉ định định dạng định dạng
[[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"1. đó là
[[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
2 - Đây là tùy chọn căn giữa giúp căn chuỗi đầu ra vào giữa khoảng trống còn lại[[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
3 - Tùy chọn dấu chỉ buộc các số âm hiển thị dấu[[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
4 - Đó là ký tự được đặt vào vị trí của khoảng trống[[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
5 - Đây là tùy chọn chiều rộng đặt chiều rộng tối thiểu của số thành 9 [bao gồm dấu thập phân, dấu phẩy hàng nghìn và dấu][[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
6 - Đây là toán tử chính xác đặt độ chính xác của số động đã cho thành 3 vị trí[[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
7 - Đây là tùy chọn loại chỉ định số là số thực
ví dụ 3. Sử dụng format[] bằng cách ghi đè __format__[]
[[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"3
đầu ra
[[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"4
Ở đây, chúng tôi đã ghi đè phương thức
[[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"8 của lớp
[[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"9
Bây giờ nó chấp nhận một tham số
# d, f and b are type
# integer
# float arguments
print[format[123.4567898, "f"]]
# binary format
0 và trả về 23 nếu nó bằng với # d, f and b are type
# integer
# float arguments
print[format[123.4567898, "f"]]
# binary format
1. Nếu không có định dạng nào được chỉ định, thì trả về # d, f and b are type
# integer
# float arguments
print[format[123.4567898, "f"]]
# binary format
2Hàm
# d, f and b are type
# integer
# float arguments
print[format[123.4567898, "f"]]
# binary format
3 chạy nội bộ # d, f and b are type
# integer
# float arguments
print[format[123.4567898, "f"]]
# binary format
4 để trả về 23Định dạng tích hợp[] Vs. Định dạng chuỗi[]
Hàm
# d, f and b are type
# integer
# float arguments
print[format[123.4567898, "f"]]
# binary format
3 tương tự như phương thức định dạng String. Trong nội bộ, cả hai phương thức đều gọi phương thức [[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"8 của một đối tượng
Mặc dù hàm
# d, f and b are type
# integer
# float arguments
print[format[123.4567898, "f"]]
# binary format
3 tích hợp là triển khai cấp thấp để định dạng đối tượng bằng cách sử dụng nội bộ [[fill]align][sign][#][0][width][,][.precision][type] where, the options are fill ::= any character align ::= "" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"8, nhưng chuỗi
# d, f and b are type
# integer
# float arguments
print[format[123.4567898, "f"]]
# binary format
3 là triển khai cấp cao hơn cũng có thể thực hiện các thao tác định dạng phức tạp trên nhiều chuỗi đối tượng