Tôi muốn biết làm thế nào để chuyển đổi một chuỗi chứa các chữ số thành gấp đôi.
Zygd
Huy hiệu vàng 19.7K3838 gold badges70 silver badges91 bronze badges
hỏi ngày 27 tháng 1 năm 2009 lúc 5:40Jan 27, 2009 at 5:40
1
>>> x = "2342.34"
>>> float[x]
2342.3400000000001
Có bạn đi. Sử dụng phao [hoạt động như nhau và có độ chính xác giống như Double C, C ++ hoặc Java].
Byte11
Huy hiệu bạc 19422 silver badges11 bronze badges
Đã trả lời ngày 27 tháng 1 năm 2009 lúc 5:49Jan 27, 2009 at 5:49
MongoosemongooseMongoose
4.5051 Huy hiệu vàng15 Huy hiệu bạc7 Huy hiệu đồng1 gold badge15 silver badges7 bronze badges
5
Toán tử thập phân có thể phù hợp hơn với những gì bạn đang tìm kiếm:
>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
Đã trả lời ngày 17 tháng 6 năm 2011 lúc 11:56Jun 17, 2011 at 11:56
foomipfoomipfoomip
1.39511 Huy hiệu bạc4 Huy hiệu đồng11 silver badges4 bronze badges
1
Xin lưu ý rằng nếu số chuỗi của bạn chứa hơn 15 chữ số quan trọng float[s]
sẽ làm tròn nó. Trong những trường hợp đó, tốt hơn là sử dụng Decimal
Đây là một lời giải thích và một số mẫu mã: //docs.python.org/3/l Library/sys.html#sys.float_info
Đã trả lời ngày 25 tháng 1 năm 2018 lúc 1:11Jan 25, 2018 at 1:11
user1767754user1767754user1767754
22.1K16 Huy hiệu vàng134 Huy hiệu bạc155 Huy hiệu đồng16 gold badges134 silver badges155 bronze badges
Sử dụng phương thức float [] hoặc phương thức thập phân [] để chuyển đổi chuỗi thành gấp đôi trong python. Chuyển đổi chuỗi thành gấp đôi giống như chuyển đổi chuỗi thành float.float[] method or decimal[] method to convert string to double in Python. Conversion of string to double is the same as the conversion of string to float. Mã ví dụ đơn giản chỉ hoạt động trên một chuỗi có một số hoặc các giá trị thập phân. Sử dụng phương thức float [] Output::s = "2.367"
res = float[s]
print[res]
print[type[res]]
Sử dụng phương thức thập phân []
Trong ví dụ này, bạn phải nhập một mô -đun thập phân.
from decimal import Decimal
s = "2.367"
res = Decimal[s]
print[res]
print[type[res]]
Hãy bình luận nếu bạn có bất kỳ nghi ngờ và đề xuất nào về chương trình đôi chuỗi Python này.
Lưu ý: IDE: & NBSP; Pycharm & NBSP; 2021.3.3 [Phiên bản cộng đồng] IDE: PyCharm 2021.3.3 [Community Edition]
Windows 10
Python 3.10.1
Tất cả & nbsp; ví dụ python & nbsp; là trong & nbsp; Python & nbsp; 3, vì vậy có thể khác với các phiên bản Python 2 hoặc nâng cấp. Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Bằng cấp về Khoa học máy tính và Kỹ sư: Nhà phát triển ứng dụng và có nhiều ngôn ngữ lập trình kinh nghiệm. Sự nhiệt tình cho công nghệ và thích học kỹ thuật.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Cho một chuỗi và nhiệm vụ của chúng tôi là chuyển đổi nó thành gấp đôi. Vì kiểu dữ liệu kép cho phép một số có các giá trị không phải là. Vì vậy, chuyển đổi chuỗi thành & nbsp; gấp đôi giống như việc chuyển đổi chuỗi thành float & nbsp;
Python3
Điều này có thể được thực hiện theo hai cách này
1] Sử dụng phương thức float []
str1
=
>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
0>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
1>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
2s = "2.367"
res = float[s]
print[res]
print[type[res]]
2s = "2.367"
res = float[s]
print[res]
print[type[res]]
3>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
1>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
2>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
3 >>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
4 >>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
5>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
1>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
2from decimal import Decimal
s = "2.367"
res = Decimal[s]
print[res]
print[type[res]]
1s = "2.367"
res = float[s]
print[res]
print[type[res]]
3Output:
This is the initial string: 9.02 The conversion of string to double is 9.02 The converted string to double is incremented by 1: 10.02
6>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
=
8>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
9Since we only want a string with a number with decimal values this method can also be used>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
Python3
>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
6=
s = "2.367"
res = float[s]
print[res]
print[type[res]]
6>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
4s = "2.367"
res = float[s]
print[res]
print[type[res]]
8Điều này có thể được thực hiện theo hai cách này
1] Sử dụng phương thức float []
str1
=
>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
0>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
1>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
2s = "2.367"
res = float[s]
print[res]
print[type[res]]
2s = "2.367"
res = float[s]
print[res]
print[type[res]]
3>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
1>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
2>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
3 >>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
4 >>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
5>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
1>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal[x]
234243.434
2from decimal import Decimal
s = "2.367"
res = Decimal[s]
print[res]
print[type[res]]
1s = "2.367"
res = float[s]
print[res]
print[type[res]]
3
Output:
This is the initial string: 9.02 The conversion of string to double is 9.02 The converted string to double is incremented by 1: 10.02