You can use negative numbers to round integers:
>>> round(1234, -3) 1000.0
Thus if you need only most significant digit:
>>> from math import log10, floor >>> def round_to_1(x): ... return round(x, -int(floor(log10(abs(x))))) ... >>> round_to_1(0.0232) 0.02 >>> round_to_1(1234243) 1000000.0 >>> round_to_1(13) 10.0 >>> round_to_1(4) 4.0 >>> round_to_1(19) 20.0
You’ll probably have to take care of turning float to integer if it’s bigger than 1.
Related Posts:
- How can I use “e” (Euler’s number) and power operation in python 2.7
- Python division
- Safest way to convert float to integer in python?
- How can I use “e” (Euler’s number) and power operation in python 2.7
- Java Round up Any Number
- What does != do/mean in python
- Clamping floating numbers in Python?
- How to perform bilinear interpolation in Python
- Inverse of Tan in python (tan-1)
- Inverse Cosine in Python
- Round *UP* to the nearest 100 in SQL Server
- Converting bytes to megabytes
- How do you round UP a number?
- How do you round UP a number?
- Converting 3D polar coordinates to cartesian coordinates
- What is the C++ function to raise a number to a power?
- Unfamiliar symbol in algorithm: what does ∀ mean? [closed]
- Python Math – TypeError: ‘NoneType’ object is not subscriptable
- numpy max vs amax vs maximum
- Integer division in Java [duplicate]
- What does the ^ (XOR) operator do? [duplicate]
- What do these operators mean (** , ^ , %, //)? [closed]
- What do these operators mean (** , ^ , %, //)? [closed]
- Round a floating-point number down to the nearest integer?
- syntaxerror: “unexpected character after line continuation character in python” math
- Limiting floats to two decimal points
- syntaxerror: “unexpected character after line continuation character in python” math
- TypeError: ‘float’ object is not callable
- How to round to 2 decimals with Python?
- Calculating a 2D Vector’s Cross Product
- Error: all the input array dimensions except for the concatenation axis must match exactly
- syntaxerror: “unexpected character after line continuation character in python” math
- How do order of operations go on Python?
- JavaScript exponents
- How to perform an integer division, and separately get the remainder, in JavaScript?
- What does numpy.gradient do?
- How to perform an integer division, and separately get the remainder, in JavaScript?
- Python: OverflowError: math range error
- How can I use numpy.correlate to do autocorrelation?
- How can I convert radians to degrees with Python?
- Infinite integer in Python
- Lua replacement for the % operator
- How do you find the rightmost digit of an integer?
- Overflow / math range error for log or exp
- How do I find the distance between two points?
- What is the fastest factorial function in JavaScript?
- How can I check for NaN values?
- Python – how to use the constant e^
- log2 in python math module
- What is the difference between ‘/’ and ‘//’ when used for division?
- How can I check for NaN values?
- What is the most efficient way to calculate the least common multiple of two integers?
- What is the most efficient way to calculate the least common multiple of two integers?
- How can I check if two segments intersect?
- How can I convert a number from base 8 to base 10?
- What is the maximum number of edges in a directed graph with n nodes?
- How to save decimal in java
- Probability of hash collision
- python math domain errors in math.log function
- How to fix ‘RuntimeWarning: divide by zero encountered in double_scalars’
- Is there a math nCr function in python?
- How do I compute derivative using Numpy?
- Is there a math nCr function in python? [duplicate]
- Rotating a Vector in 3D Space
- Function to determine if two numbers are nearly equal when rounded to n significant decimal digits
- Mathematica matrix diagonalization
- How do I copy a file in Python?
- Pig Latin Translator
- if/else in a list comprehension
- data type not understood
- How to read a text file into a string variable and strip newlines?
- What does hash do in python?
- How to copy a dictionary and only edit the copy
- How to change default Python version?
- How to customize a scatter matrix to see all titles?
- What is the problem with shadowing names defined in outer scopes?
- TypeError: write() argument must be str, not bytes (Python 3 vs Python 2 )
- Array ArrayList python equivalent
- How to clear the interpreter console?
- Using python’s eval() vs. ast.literal_eval()
- How can I stop a While loop?
- Pandas: change data type of Series to String
- Python [Errno 98] Address already in use
- How do you call an instance of a class in Python?
- Flask ImportError: No Module Named Flask
- How do you use subprocess.check_output() in Python?
- Download large file in python with requests
- Count unique values per groups with Pandas [duplicate]
- Pytesseract : “TesseractNotFound Error: tesseract is not installed or it’s not in your path”, how do I fix this?
- Multiprocessing vs Threading Python
- How to get line count of a large file cheaply in Python?
- Getting
with Python http requests instead of INT - Is log(n!) = Θ(n·log(n))?
- How to round float numbers in javascript?
- Difference between data type ‘datetime64[ns]’ and ‘
- Merging two DataFrames
- CSV new-line character seen in unquoted field error
- How to change python version in Anaconda?
- TypeError: ManyRelatedManager object is not iterable
- vectorize conditional assignment in pandas dataframe