mhking04 Posted September 16, 2004 Share Posted September 16, 2004 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 More sharing options...
0 kjordan2001 Posted September 16, 2004 Share Posted September 16, 2004 Language? Although casting would probably work. int x = (int)dint; Link to comment Share on other sites More sharing options...
0 mhking04 Posted September 16, 2004 Author Share Posted September 16, 2004 hmm, maybe that woulda helped, my bad im using Java. Link to comment Share on other sites More sharing options...
0 _kane81 Posted September 16, 2004 Share Posted September 16, 2004 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 More sharing options...
0 mhking04 Posted September 16, 2004 Author Share Posted September 16, 2004 ok, so can you format it so that it will only show 2 decimal places? Link to comment Share on other sites More sharing options...
0 ItchyAnus Posted September 16, 2004 Share Posted September 16, 2004 Use NumberFormat and DecimalFormat. Link to comment Share on other sites More sharing options...
0 mhking04 Posted September 16, 2004 Author Share Posted September 16, 2004 could you please explain that, i do not understand Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted September 16, 2004 Share Posted September 16, 2004 DecimalFormat df = new DecimalFormat("######.##"); String s = df.format(number); Link to comment Share on other sites More sharing options...
0 +virtorio MVC Posted September 16, 2004 MVC Share Posted September 16, 2004 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 More sharing options...
0 em_te Posted September 16, 2004 Share Posted September 16, 2004 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 More sharing options...
0 Deliorion Posted September 16, 2004 Share Posted September 16, 2004 For Java you can just use x = Math.round(#); I think. Link to comment Share on other sites More sharing options...
0 Kestrel Posted September 16, 2004 Share Posted September 16, 2004 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. Which is why in this case you add 0.005 first... Link to comment Share on other sites More sharing options...
0 Radium Posted September 16, 2004 Share Posted September 16, 2004 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. Link to comment Share on other sites More sharing options...
0 MrA Posted September 17, 2004 Share Posted September 17, 2004 (edited) 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 September 17, 2004 by MrA Link to comment Share on other sites More sharing options...
0 Radium Posted September 17, 2004 Share Posted September 17, 2004 I hate to be an ######, but floats are a subset of the reals. They's not reals themselves. Si! :cool: Link to comment Share on other sites More sharing options...
0 Lazlo Woodbine Posted September 17, 2004 Share Posted September 17, 2004 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 More sharing options...
0 MrA Posted September 17, 2004 Share Posted September 17, 2004 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 More sharing options...
0 Lazlo Woodbine Posted September 17, 2004 Share Posted September 17, 2004 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 More sharing options...
0 kjordan2001 Posted September 17, 2004 Share Posted September 17, 2004 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 More sharing options...
0 MrA Posted September 17, 2004 Share Posted September 17, 2004 You know, you guys are right, I'm wrong, I'm sorry. Maybe this is why I almost failed algebra, my incredebly bad logic. Anywho, I'm sleepy. Link to comment Share on other sites More sharing options...
0 Lazlo Woodbine Posted September 17, 2004 Share Posted September 17, 2004 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 More sharing options...
0 Shadrack Posted September 18, 2004 Share Posted September 18, 2004 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 More sharing options...
0 Andareed Posted September 18, 2004 Share Posted September 18, 2004 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 More sharing options...
0 _kane81 Posted September 19, 2004 Share Posted September 19, 2004 Maths .... Yuck! Set Theory ..... Yuck! Do I really need maths to do programming (except in research)..... no - Thank God or else I'd be screwed. Link to comment Share on other sites More sharing options...
Question
mhking04
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