英文:
how to save to MYSQL in Spring boot with decimals if I save 10,000.00 it will be saved as 10 in MySQL. (Save only the numbers in front of comma.)
问题
Data I enter
Data that get SavedSevice Code
我尝试保存时不包括小数点,并在显示时加上小数点,但未成功。考虑将相同的值保存在新文本框中,并在保存时不包括逗号。我认为逗号是问题所在,而我选择的数据类型是双精度。
英文:
Data I enter
Data that get SavedSevice Code
I tried to save without the decimals and display it with the decimal it did not work. Thinking of saving the same value in a new textbox and saving it while making the new text box without comma. I think the comma is the problem hear and the data type I have chosen is double.
答案1
得分: 2
是的,MySQL不会将逗号视为数字的一部分。如果启用了STRICT_TRANS_TABLES SQL模式(这是较新版本的默认设置),当您插入或更新带有这种值的数字字段时,将会出现错误。没有启用STRICT_TRANS_TABLES,您只会收到警告,并且非数字部分将被丢弃,只留下 10
。
英文:
Yes, mysql will not recognize the comma as part of a number. If you have the STRICT_TRANS_TABLES sql_mode enabled (which is the default in newer versions), you will get an error when you insert or update to a numeric field with such a value. Without STRICT_TRANS_TABLES, you will only receive a warning, and the non-numeric portion will be discarded, leaving just 10
.
答案2
得分: 0
你自己编写了一个GUI吗?如果是的话,那么你正在向后端发送错误的类型。如果你的GUI是用JavaScript编写的,并且你正在输入带逗号的数字,请不要直接发送输入字段的value
,而是发送Number(value.replace(",", ""))
。同时,确保在Spring Boot中,该字段不是Long或Integer类型,而应该在后端使用Float或Double类型来处理浮点数。
英文:
Did you code a GUI yourself? If yes, then you are sending wrong type to your backend. If your GUI written in javascript and you are entering numbers with comma dont send the value
of input directly, instead send Number(value.replace("," , ""))
. And also check the field is not in type Long or Integer in Spring Boot. You should use Float or Double type in backend for floating numbers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论