3 examples of 'how to round to the nearest tenth in python' in Python

Every line of 'how to round to the nearest tenth in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
50def closest(number, ndigits=0, resolution=None):
51 """Round a number defining the number of precision decimal digits and
52 the resolution.
53
54 Examples
55 --------
56
57 >>> closest(103.66778,)
58 104.0
59 >>> closest(103.66778, ndigits=2)
60 103.67
61 >>> closest(103.66778, ndigits=2, resolution=5)
62 105.0
63 >>> closest(103.66778, ndigits=2, resolution=0.5)
64 103.5
65 >>> closest(103.66778, ndigits=2, resolution=0.25)
66 103.75
67 """
68 num = round(number, ndigits)
69 return (num if resolution is None else
70 round((num // resolution * resolution +
71 round((num % resolution) / float(resolution), 0) *
72 resolution), ndigits))
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

248def nearest(n):
249 """
250 round up or down to nearest int returning an int
251
252 Used for selecting the nearest pixel position.
253
254 WARNING, this method is vectorised to handle a numpy array by NumpyImage
255
256 :param a number or array of numbers: virtual pixel position (x or y)
257 :return int: nearest integer (round up/down)
258 """
259 if type(n) is None: return
260 n=float(n) # make sure n is a float
261 return int(round(n,0))
8def round_to_ten(d):
9 t = int(d//10)*10
10 if t==180: return -180
11 return t

Related snippets