Python Forum
Get numpy ceil and floor value for nearest two decimals
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get numpy ceil and floor value for nearest two decimals
#1
I'm trying to get ceiling value of the input for two decimals.


import numpy as np
a = np.array([-1.784, -1.5888, -0.24444, 0.25555, 1.5, 1.75, 2.0])

np.around(a,2)
array([-1.78, -1.59, -0.24, 0.26, 1.5 , 1.75, 2. ])

np.ceil(a)
array([-1., -1., -0., 1., 2., 2., 2.])

np.floor(a)
array([-2., -2., -1., 0., 1., 1., 2.])

Is there a way I can use Numpy ceil and floor methods to get such values to nearest second decimal?
Reply
#2
you can use np.around : numpy.around(a, decimals=2, out=None) (see np.around)
klllmmm likes this post
Reply
#3
Why not this ?
>>> import numpy as np
>>> a = np.array([-1.784, -1.5888, -0.24444, 0.25555, 1.5, 1.75, 2.0])
>>>  
>>> np.floor(100 * a)/100
array([-1.79, -1.59, -0.25,  0.25,  1.5 ,  1.75,  2.  ])
>>> np.ceil(100 * a) / 100
array([-1.78, -1.58, -0.24,  0.26,  1.5 ,  1.75,  2.  ])
Reply
#4
import numpy as np

# Input array
arr = np.array([-2.025, -2.123, -1.456, 0.789, 1.234, 1.567, 2.345])

# Round values to the nearest second decimal using ceil and floor methods
ceil_values = np.ceil(arr * 100) / 100
floor_values = np.floor(arr * 100) / 100

print("Ceil values:", ceil_values)
print("Floor values:", floor_values)

In this example, we multiply the array by 100 to shift the decimal point two places to the right. Then, we apply the ceil and floor functions to round the values to the nearest whole number. Finally, we divide the values by 100 again to shift the decimal point back to the original position, resulting in values rounded to the nearest second decimal.
Reply
#5
As I suggested:
import numpy as np
a = np.array([-1.784, -1.5888, -0.24444, 0.25555, 1.5, 1.75, 2.0])
a_rounded = np.around(a, decimals=2)
print(f"{a_rounded}")
Output:
[-1.78 -1.59 -0.24 0.26 1.5 1.75 2. ]
Actually I'm just wondering if that's your need or if you're asking as well for "zeros" to complete decimals; for instance "1.5" and "2." must provide "1.50" and "2.00" respectively
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python won 't do decimals SomebodySmart 5 728 Jun-08-2023, 07:14 PM
Last Post: buran
  compare and find the nearest element ? mr_gentle_sausage 4 1,055 Jan-15-2023, 07:11 AM
Last Post: DPaul
  Floor approximation problem in algorithm gradlon93 3 960 Dec-14-2022, 07:48 PM
Last Post: Gribouillis
  Adding Decimals to classes with OOP + rounding to significant digits (ATM demo) Drone4four 7 2,324 May-04-2022, 06:15 AM
Last Post: Drone4four
  Floor division problem with plotting x-axis tick labels Mark17 5 2,118 Apr-03-2022, 01:48 PM
Last Post: Mark17
  floats 2 decimals rwahdan 3 1,641 Dec-19-2021, 10:30 PM
Last Post: snippsat
  Converting decimals stylingpat 3 2,136 Mar-27-2021, 02:32 PM
Last Post: deanhystad
Photo Locate Noise floor level for a spectral data in Python Ranjan_Pal 1 3,065 Dec-19-2020, 10:04 AM
Last Post: Larz60+
  Floor division return value Chirumer 8 3,797 Nov-26-2020, 02:34 PM
Last Post: DeaD_EyE
  Rounding to the nearest eight wallgraffiti 2 2,096 Jul-15-2020, 06:05 PM
Last Post: wallgraffiti

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020