• 0

Rounding off integers


Question

in my program, i have set up a double integer.

after all the calculations are done, it displays it on the screen as a very long integer, the decimal places being very long.

how do i simply round off the number and tell it to only show it to two decimal places ?

cheers

Link to comment
Share on other sites

23 answers to this question

Recommended Posts

  • 0

What language are you programming in?

on another note, you dont have an interger value, you have a float/double value which will give you decimal places. Integer/long types dont have decimal places.

Link to comment
Share on other sites

  • 0

Or you could just do this:

double value = 34.564636565;
long temp = value * 100;
value = (double)temp / 100;

rounds to 2 decimal places.

You never want to get too relyant on classes/API's because you wont be able to do this sort of thing without them.

Link to comment
Share on other sites

  • 0
double value = 34.564636565;
long temp = value * 100;
value = (double)temp / 100;

Gotta be careful though. Converting a double to an integer/long doesn't round the number. So a 0.009 gets converted to a 0.00.

Link to comment
Share on other sites

  • 0
Double = Float, not integer.

Float <> Integer

A double is a 64-bit float.

A single is a 32-bit float.

Floats are called "real" in real life.

I hate to be an ######, but floats are a subset of the reals. They's not reals themselves.

Edited by MrA
Link to comment
Share on other sites

  • 0
I hate to be an ######, but floats are a subset of the reals.  They's not reals themselves.

Huh? :blink: That's a completely contradictory pair of statements you've made.

If floats are a *subset* of the reals, then they must be reals.

If floats are *not reals*, then they can't be a subset of them.

Link to comment
Share on other sites

  • 0
Huh? :blink: That's a completely contradictory pair of statements you've made.

If floats are a *subset* of the reals, then they must be reals.

If floats are *not reals*, then they can't be a subset of them.

No, floats are a subset of reals because they contain a limited number of numbers, all of which are reals. Floats are not themselvers reals because if they were, then every possible real number (a lot of numbers) would be possible in a float which is not the case.

Anywho, I've this topic off-topic and I apologise for doing so.

Link to comment
Share on other sites

  • 0
No, floats are a subset of reals because they contain a limited number of numbers, all of which are reals.

What you've just said is that floats are reals!!

Floats are not themselves reals because if they were, then every possible real number (a lot of numbers) would be possible in a float which is not the case.

Your conclusion in that statement is completely illogical and wrong. Simply being a group of reals does NOT imply that all reals are in that group.

Link to comment
Share on other sites

  • 0
No, floats are a subset of reals because they contain a limited number of numbers, all of which are reals.  Floats are not themselvers reals because if they were, then every possible real number (a lot of numbers) would be possible in a float which is not the case.

Anywho, I've this topic off-topic and I apologise for doing so.

lol, the meaning of a subset is that it can be a portion of the set or even all of the set. Plus how can they be a subset and not be a part of the set?

Link to comment
Share on other sites

  • 0
You know, you guys are right, I'm wrong, I'm sorry.

Maybe this is why I almost failed algebra, my incredebly bad logic.

Your spelling's not much better. :devil: (incredibly)

Seriously though, no real need to apologise.

I now know a little bit more than I did before the start of this thread, and it seems you do too. That's a good thing! :D

Anywho, I'm sleepy.

Sleep well. :)

Link to comment
Share on other sites

  • 0

yeah,

Digital computers are, well digital. There is no such digital hardware that can represent any real number. Floating point numbers are the closest and the math isn't perfect. But its good enough (well, is it good enough?). If you really need real numbers you need an analog computer. But they aren't very user friendly, and can be rather expensive. However, they can aid in solving systems (that need real precision).

Wow, I?m way OT

Link to comment
Share on other sites

  • 0

That's not quite true. The number 1 (and any integer) is a real number. If you want real (no pun intended) precision, you would have to use some system/software that deals purely with rationals and radicals without representing them as finitely precise decimals.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.