java - Parse double from JFormattedTextField -


I have a bug or something I have a method that saves an article such as:

  class executes the SaveArticleListener implementation {// .... string s = textArticlePrice.getText (). Replace (',', '') .replaceAll ("\\ s", ""); Double price = double. Prsd double (s); // ....}   

Where textArticlePrice is a JFormattedTextField that is configured:

  number format suffix format = number format.getnumberinstance (); PriceFormat.setMaximumFractionDigits (2); PriceFormat.setMinimumFractionDigits (2); TextArticlePrice = new JFormattedTextField (priceFormat); TextArticlePrice.setColumns (10); I get every time in   

and parseDouble method:

  java.lang.NumberFormatException: for input string: "123 456 789.00"   

then works with the change dot, but not white space ... why?

To parse you string , your NumberFormat will be better. Refer to priceFormat , and then use

  double price = priceFormat.parse (textArticlePrice.getText ()). DoubleValue ();   

The formattor being used to display numbers is the same way that it is converted back to double so that you know it's parsing it Being is a consistent way.

Best of all

  Dual value = ((number) textArticlePeace.GateValue ()). Double Values ​​();   

If you have set your JFormattedTextField correctly, then it should work without any need for conversion ( getValue () The call returns a object , so you have to enter it. It is based on a double or flame , which is in the text field The safe way to get the double is to consider it as the number , which is both the super administrator, and its .doubleValue ( ) Call the method.)

Write something so that it can be parsed with Double.parseDouble () . Actually there is not a right way to go because it is very weak if the format of your text field changes later

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -