-4

I've the following number: 14.2423421. This number is a float. I want to convert it into 142423421. So, I want the same number, without the point. It has to be an int. Obviously it mustn't be a string type. How to do it? Thank you everybody!

Antonio
  • 3
  • 1
  • 2
    You don't have only this number, do you? How would you want to convert _1.1_? – Armali Aug 07 '21 at 08:54
  • into 11, i simply would like to remove the point. I would convert 1.1 into the int 11. – Antonio Aug 07 '21 at 08:56
  • 2
    And if `x` is a `float` defined with `float x = 14.242342f;`, what integer value would you want to convert `x` to? Bear in mind that on most systems, `14.2423421f` and `14.242342f` will have the *same* value. Backing up a step, maybe you can explain what this is for? There may be a better way of solving the underlying problem, whatever it is. – Mark Dickinson Aug 07 '21 at 09:09
  • 2
    The concept of "removing the point" seems simple and intuitive if you consider the number as a string. However, that concept makes no sense when you consider the number....as a number. What mathematical operation can you possibly do to "remove a point" in the way you described? Even worse, due to the floating point representation and imprecision that comes with it - the "intuitive" result is often not going to be achievable. Perhaps you should represent the numbers *directly* as strings instead. – Chase Aug 07 '21 at 09:32
  • 1
    This question uses a flawed model of floating-point. In the format most commonly used for `float`, 14.2423421 is not representable. The closest representable value is 14.2423419952392578125. So the program does not have the number 14.2423421, and there is no information in a `float` that can tell us that an input of 14.2423419952392578125 ought to produce 142423421 rather than 142423419952392578125. – Eric Postpischil Aug 07 '21 at 11:08
  • 14.2423421 is not a float. It is a decimal representation of a float. Internally, there is no decimal, no point, and no spoon. – stark Aug 07 '21 at 12:25
  • 2
    So 1.1 -> 11, right? What about 1.10? should it be 110 or 11? I suspect your number should never have been stored as floating point in the first place. – HAL9000 Aug 07 '21 at 13:20
  • If you were asking this question out of curiosity, to learn more about how floating-point numbers are represented and how to work with them, I hope these comments have shown you: the task is impossible! It's not not because it's too hard or we're not smart enough to figure out how to do it -- rather, it turns out the question doesn't actually make any sense, given the way floating-point numbers are represented internally, which is as binary floating-point. The question would really only make sense for fixed-point decimal fractions. – Steve Summit Aug 07 '21 at 14:52
  • If you were asking this question as part of an assignment or project you're working on, there's something badly wrong with that assignment or project (again, because this task is impossible). If you tell us more about the assignment or project, we might be able to untangle this impossible requirement and figure out what you should really be trying to do. – Steve Summit Aug 07 '21 at 14:53
  • For a number like 14.34, which is represented internally as a binary `float` which is closer to 14.3400001525, you *might* be able to write some code to decide that those four 0's in a row are "close enough", and that the 1525 part "doesn't matter", and that the answer should be 1434. But for 14.2423421, which as Eric P. has explained is internally closer to 14.242341995, I don't know how you'd decide to print 14242342, or 142423419, or 1424234199, or some other number. – Steve Summit Aug 07 '21 at 15:01
  • Even 14.24 is problematic -- internally it's closer to 14.23999977, so you'd have to somehow decide that those four 9's in a row mean you ought to "round up" to 1424. – Steve Summit Aug 07 '21 at 15:04
  • Here's another "what if" question. (If you're not tired of them already. I'm sorry, you probably are tired of them already.) What if the number you're working with is 1/3? Should the answer be 13, or 133, or 1333, or...? – Steve Summit Aug 07 '21 at 15:12
  • And, finally, here's an answer. It doesn't work perfectly, but it actually works kinda okay, and in its imperfection it illustrates the fundamental difficulties. Given an input float number `f`, just do `while(fabs(f - (int)f) > 0.001) f *= 10;` followed by `int i = f + 0.5;` to round off to the nearest `int`. That number `0.001` in there essentially means that if there are three 0's in a row, or three 9's, we're "close enough". But it doesn't work for the example number 14.2423421, which it converts to 14242342, which it decides is "close enough". – Steve Summit Aug 07 '21 at 15:42

1 Answers1

0

Count the number of decimals as suggested here and multiply your number by 10^(number of decimals)

fifoq
  • 183
  • 13
  • This is actually a very good answer to the question as stated. It won't work very well, but that's because the question as stated is flawed. But if you're bound and determined to "remove the decimal point", something like this is the best you can do. (Nice job finding the [linked question](https://stackoverflow.com/questions/1083304/c-c-counting-the-number-of-decimals).) You will of course have to round off the result at the end, because often (roughly half the time!) it will be a number ending in .999, and you'll want to round up to the nearest integer. – Steve Summit Aug 07 '21 at 15:35