วิธีแปลง 1,000 เป็น 1000 python (convert 1,000 to 1000)

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: วิธีแปลง 1,000 เป็น 1000 python (convert 1,000 to 1000)

วิธีแปลง 1,000 เป็น 1000 python (convert 1,000 to 1000)

โดย tatiya » 03/12/2018 4:24 pm

ฉันจะใช้ Python เพื่อแปลงสตริงให้เป็นตัวเลขได้อย่างไรถ้ามีเครื่องหมายจุลภาคในตัวคั่นหลายพันตัว?

วิธีก็คือ

โค้ด: เลือกทั้งหมด

a = "1,000"
b = int(a.replace(',', ''))
print(b)
ผลลัพท์
1000

โค้ด: เลือกทั้งหมด

a = "1,000,000"
b = int(a.replace(',', ''))
print(b)
ผลลัพท์
1000000

ข้างบน