round() 메서드는 n자리로 반올림된 값의 소수점을 반환합니다.
구문
다음은 round() 메서드의 구문입니다.
round( 소수점 뒤에 유지되는 자릿수를 나타내는 숫자 값
반환 값
이 메서드는 값의 소수점을 반환합니다. 정수 부분을 유지한다고 합니다.).nggits
Example
다음 예는 round() 메서드의 사용을 보여줍니다
#!/usr/bin/python2 print "round(80.23456, 2) : ", round(80.23456, 2) print "round(100.000056, 3) : ", round(100.000056, 3) print "round(-100.000056, 3) : ", round(-100.000056, 3)
위 프로그램을 실행하면 다음과 같은 결과가 나타납니다.
round(80.23456, 2) : 80.23 round(100.000056, 3) : 100.0 round(-100.000056, 3) : -100.0
Python3과 Python2 버전의 차이점
Python2는 x를 0에서 멀어지는 가장 가까운 배수로 반올림합니다(예: round(0.5)=1, round(-0.5)) =-1;
Python3 그런 다음 x는 가장 가까운 짝수 배수로 반올림됩니다(예: round(0.5)=0, round(1.5)=2.0, round(2.5)=2.0
코드:
#!/usr/bin/python2 print round(2.635, 2) print round(2.645, 2) print round(2.655, 2) print round(2.665, 2) print round(2.675, 2)
출력 결과:
2.63 2.65 2.65 2.67 2.67
둥근법 결함
위의 예를 보면 우리가 알고 있는 것과는 반올림의 반올림 방법이 다른 것을 알 수 있습니다. 사실 이것은 반올림의 버그가 아닙니다. 이것은 주로 십진법의 변환에서 비롯됩니다. 입력 시 컴퓨터의 내부 바이너리에 저장되며 이 문제는 제한된 정밀도로 해결이 불가능하며 해결할 필요가 없습니다.
Python의 10진수 모듈을 사용하면 이 문제를 해결할 수 있습니다.
반올림할 필요가 없다면 가장 친숙한 인쇄("%.2f" % 2.675) 방법을 사용하는 것도 고려해 볼 수 있습니다.
위 내용은 python2와 python3의 반올림 차이점 소개의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

forhandlinglargedatasetsinpython, usenumpyarraysforbetterperformance.1) numpyarraysarememory-effic andfasterfornumericaloperations.2) leveragevectorization foredtimecomplexity.4) managemoryusage withorfications data

inpython, listsusedyammoryAllocation과 함께 할당하고, whilempyarraysallocatefixedMemory.1) listsAllocatemememorythanneedInitiality.

Inpython, youcansspecthedatatypeyfelemeremodelerernspant.1) usenpynernrump.1) usenpynerp.dloatp.ploatm64, 포모 선례 전분자.

numpyissentialfornumericalcomputinginpythonduetoitsspeed, memory-efficiency 및 comperniveMathematicaticaltions

contiguousUousUousUlorAllocationScrucialForraysbecauseItAllowsOfficationAndFastElementAccess.1) ItenableSconstantTimeAccess, o (1), DuetodirectAddressCalculation.2) Itimprovesceeffiency theMultipleementFetchespercacheline.3) Itsimplififiesmomorym

slicepaythonlistisdoneusingthesyntaxlist [start : step : step] .here'showitworks : 1) startistheindexofthefirstelementtoinclude.2) stopistheindexofthefirstelemement.3) stepisincrementbetwetweentractionsoftortionsoflists

NumpyAllowsForVariousOperationsOnArrays : 1) BasicArithmeticLikeadDition, Subtraction, A 및 Division; 2) AdvancedOperationsSuchasmatrixmultiplication; 3) extrayintondsfordatamanipulation; 5) Ag

Arraysinpython, 특히 Stroughnumpyandpandas, areestentialfordataanalysis, setingspeedandefficiency


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

Dreamweaver Mac版
시각적 웹 개발 도구