0

I have rather unusual request, but I hope someone might be able to help.

I am appending floats as values to numpy array. I would like to make sure that float does not get rounded when its decimal part ends in 0. For example: I would like float 31.30 to stay 31.30 in the array, but what I am experiencing now is that it gets set to 31.3.

The reason why I want to make sure float does not change is that later on I am splitting it to two integers(31.30 to 31 and 30) and it is of critical importance for me that both integers have the 'complete' values.

Is there any way to get around this? I tried with making these floats strings, but my problem is that I need to compare the arrays with these floats and numpy.array_equal() does not seem to work for arrays of strings...

Any help will be greatly appreciated,

Best wishes

3
  • 2
    31.3 and 31.30 are exactly identical numbers. How do you expect them to differ in any way? Commented Feb 21, 2014 at 0:45
  • Removing a non significant 0 is not called rounding. I don't know what you try to achieve, but this is not the right way. You should think of a better data structure and algorithm. Maybe if you explain more in details we can help you further. Commented Feb 21, 2014 at 0:51
  • To put it another way -- a float does not remember the specific literal you used to define it. 31.3 == 31.30, they are just two different ways of representing the same value. It's not much different from the fact that the 3 you get from writing x = 3 is the same as the 3 you get from writing x = 1 + 2. Commented Feb 21, 2014 at 2:39

2 Answers 2

3

Since 31.3 and 31.30 are exactly the same number, there is no way to distinguish them. But I guess you don't need to anyway. This seems more like a formatting issue. If you always expect two digits after the dot:

from math import floor

x = 31.3

whole_part = int(floor(x))
two_after_dot = int(floor(x*100))-whole_part*100

print(whole_part, two_after_dot)

Output:

31 30

If this actually isn't the case and the number of digits after the dot should vary while also keeping varying numbers of trailing zeros, then you cannot use numeric types. Use strings from the very beginning instead.

Sign up to request clarification or add additional context in comments.

Comments

0

When I ran into a similar problem because of converting millisecond references to microseconds, I had to convert to a string and loop over the string adding the needed 0's until the length of the string was correct. Then when the value was converted back to integer, the calculations worked.

The data will be passed to strptime as as string
vals = vals.split('.') # a fractional part of the seconds
nofrag, frag = vals
length = len(frag)
# This converts the fractional string to microseconds, given unknown precision
if length > 6:
  frag = frag(0:5)
else:
  while length < 6:
    frag = frag + '0'
    length += 1
# strptime requires even seconds with microseconds added later
nofrag_dt = DT.datetime.strptime(nofrag, '%Y%m%d %H%M%S')
dt = nofrag_dt.replace(microsecond=int(frag))
return dt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.