hello, I'm doing a web CRUD and I have an error: When I insert a new row if the "price" field contains decimals, it throws me NumberFormatException Searching in Google I discovered that the error is because the INPUT NUMBER of the HTML that I implemented uses "," to separate between integers and decimals and apparently java requires "." to be used. So when I cast the String , which comes from the request that captures the input to convert it to double, it throws me that error. I tried to solve this in a somewhat dirty way using the .replaceAll and changing the "," by "." as follows:
double precio = Double.parseDouble (request.getParameter("precio").replaceAll(",", "."));
The funny thing is that the exception no longer throws me but now it loads subtly different numbers with an exaggerated number of decimals that it simply invents. For example: I loaded in the input 23.11 and as a result I got 23.110000610351562 in the SQL database and so on. It should be noted that I tried to use float and it threw me other errors. Another strange thing is that I have seen tutorials where there is no problem with the use of double and database and they should not make any detours to adapt it. Why do I have problems and others don't? How can I solve that?
From what I see you have several problems the 1st is that the INPUT NUMBER that you use if it is not the native HTML must have an additional api a file that you did not download with the regional configuration and once you have it added to your project you have to do something like this:
Here use a
select2
for JQuery and adatetimepicker
. For both download your language locales files and initialize them with the code above. For example forselect2
your file wasselect2-i18n/es.js
. For himdatepicker.min.js
there is a filedatepicker.es-ES.min.js
and for himdatetimepicker
there was a file with all the languagesjquery.datetimepicker.full.min.js
Since you don't specifically put which one you use I can't tell you how to do that exactly.The 2nd thing you have to check is the regional settings of your operating system. In Windows go to "Edit language and keyboard options" -> "Date, time and regional format"->"Additional date, time and regional options"->In region "Change date, time or number formats" or go directly to the control panel and search for Region and then "Change date, time or number formats":
Then you go to additional settings:
Change the decimal symbol to "." And the thousands separator symbol by ",". These settings will end up affecting your IDE and java itself.
The 3rd problem I see is that your field is called "price", for which you are going to use money and therefore you cannot use
Double(64 bits)
neitherFloat(32 bits)
since they are resolved internally with 2's complement and end up rounding. To manage money you must use the classjava.math.BigDecimal
, because it is useless to use typesmoney
ormoneda
neither in java nor in databases to manage money.So it would be something like this:
and then
That is, if you fix the locale of your INPUT NUMBER by retrieving it in java, you just have to remove the "," separating thousands and use the value to create a
BigDecimal
.Another thing is that the javascript usually has the browser language by default, if you downloaded a browser configured for Spanish from Spain, it may be the reason why the decimal separator symbol is "," and not ".". If this is the case, you will have to download one configured with international or Latin American Spanish.
You have to check all these points to correct your problem.