Results 1 to 17 of 17
Thread: Rounding to n decimal place.
- 02-26-2013, 05:21 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
- 02-26-2013, 05:23 PM #2
Re: Rounding to n decimal place.
Doing a google search of "java rounding" comes up with a ton of results. Were you having trouble with a particular approach?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
-
Re: Rounding to n decimal place.
Also, you may wish to clarify your question: are you talking about rounding a number or rounding the String representation of a number? Often folks mention the first but really mean the second.
- 02-26-2013, 05:43 PM #4
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Re: Rounding to n decimal place.
I did google that. Because i need to put a variable in for the current value which should be displayed on my calculator and how many decimal points i want it to be rounded to. I was wondering if there was any method which took these two parameters? Which I couldn't find on google. Is there any method for this?
I tried Math.round but that didn't let me round to any decimal points.
I am rounding a number.
Thanks
-
Re: Rounding to n decimal place.
It's as I suspected. You should not look towards rounding the number but rather the String display of the number. Look at using a DecimalFormatter object or the String.format(...) method. You can find much about both of these guys via Google, but if you're still stuck, please post your attempt here and let's work with you on this.
This tries to round the number itself which is not what you need.I tried Math.round but that didn't let me round to any decimal points.
No, you're not actually.I am rounding a number.
-
Re: Rounding to n decimal place.
For example:
This prints out:Java Code:import java.text.DecimalFormat; public class NumberFormatEg { public static void main(String[] args) { double foo = 1.0/3.0; System.out.println("foo: " + foo); DecimalFormat decimalFormat = new DecimalFormat("0.###"); System.out.println("foo formatted: " + decimalFormat.format(foo)); } }
foo: 0.3333333333333333
foo formatted: 0.333
- 02-27-2013, 09:54 PM #7
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Re: Rounding to n decimal place.
Hello, i tried it and it kind of worked. Am i allowed to put a variable into the parameters of the DecimalFormat creation? As long as it is the right string format? As i have a loop which adds a "#" to a variable which is originally "#." every time i need the rounding to a decimal point to go up by one.
- 02-28-2013, 03:00 PM #8
Re: Rounding to n decimal place.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-02-2013, 02:06 PM #9
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Re: Rounding to n decimal place.
i get this error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Number
-
Re: Rounding to n decimal place.
You will probably want to post demo code to show what you're actually trying to do, along with the actual error message. The best code to post is a small self-contained program that we can try to run that demonstrates for us the error, similar to the small program that I posted above.
- 03-02-2013, 03:10 PM #11
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Re: Rounding to n decimal place.
this is the error i get:Java Code:public class Calculator { private boolean decimal; private double currentValue; private double previousValue; private double timesNumber; private double functionValue; private int timesNumberCondition1; private int timesNumberCondition2; private int DFTextCondition1; private int DFTextCondition2; private DecimalFormat decimalFormat; private String DFText; public Calculator(){ decimal = false; currentValue = 0.0; previousValue = 0.0; timesNumber = 1.0; functionValue = 0.0; timesNumberCondition1 = 0; timesNumberCondition2 = 1; DFTextCondition1 = 0; DFTextCondition2 = -1; decimalFormat = new DecimalFormat("DFText"); DFText = "#.#"; } public void afterFunction() { decimal = false; timesNumberCondition1 = 0; timesNumber = 1.0; timesNumberCondition2 = 1; DFText = "#.#"; DFTextCondition1 = 0; DFTextCondition2 = -1; } public void dot() { decimal = true; } //this method is used to add the correct amount of "#"s for the current number, i have got that to work. public void number(int n) { if(decimal == false) { currentValue = currentValue * 10 + n; } else { while(timesNumberCondition1 < timesNumberCondition2){ timesNumber = timesNumber * 10; timesNumberCondition1++; if(timesNumberCondition1 > 1){ while(DFTextCondition1 <= DFTextCondition2){ DFText = DFText + "#"; System.out.println(DFText); DFTextCondition1++; } } } previousValue = currentValue; currentValue = n / timesNumber; currentValue = currentValue + previousValue; timesNumberCondition2++; DFTextCondition2++; } } //when this is returned i get the error message public String currentDisplay() { String text = Double.toString(currentValue); text = decimalFormat.format(text); return text; } }
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(DecimalFormat.java: 505)
at java.text.Format.format(Format.java:157)
at Calculator.currentDisplay(Calculator.java:203)
at CalculatorGUI.actionPerformed(CalculatorGUI.java:1 77)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.jav a:6505)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
at java.awt.Component.processEvent(Component.java:627 0)
at java.awt.Container.processEvent(Container.java:222 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
at java.awt.Component.dispatchEvent(Component.java:46 87)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719 )
at java.awt.Component.dispatchEvent(Component.java:46 87)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:703)
at java.awt.EventQueue.access$000(EventQueue.java:102 )
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:676)
at java.awt.EventQueue$4.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 673)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:97)
calculatorGUI is just a class which calls the methods.
- 03-02-2013, 03:20 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
- 03-02-2013, 03:33 PM #13
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Re: Rounding to n decimal place.
I changed it to:
but i still get the same error. I really don't know what i'm doing wrong.Java Code:DFText = "#.#"; decimalFormat = new DecimalFormat(DFText);
Thanks.Last edited by HarleyRowland; 03-02-2013 at 03:36 PM.
- 03-02-2013, 04:10 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Re: Rounding to n decimal place.
Your method in line #69 tries to format a String; it can't do that, it can only format an object of type Number (or a subclass thereof); autoboxing takes care that you can format a primitive double.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-02-2013, 06:25 PM #15
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Re: Rounding to n decimal place.
Wow thankyou! it works now spent ages trying to work that out. Is it okay to delete my code in post above?
- 03-02-2013, 08:48 PM #16
- 03-03-2013, 06:34 AM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Re: Rounding to n decimal place.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Rounding to one decimal place
By threlot in forum New To JavaReplies: 9Last Post: 10-16-2012, 05:52 AM -
Rounding Decimal Places
By neverbend in forum JCreatorReplies: 4Last Post: 09-29-2011, 11:41 PM -
Rounding decimal error
By NixasMuraki in forum New To JavaReplies: 3Last Post: 06-01-2011, 01:07 AM -
Compile Error (decimal rounding attempt)
By mylosol in forum New To JavaReplies: 2Last Post: 05-08-2011, 11:13 PM -
rounding double to two decimal places
By javaMike in forum Advanced JavaReplies: 15Last Post: 03-10-2010, 12:04 AM


LinkBack URL
About LinkBacks


Bookmarks