简体   繁体   English

如何在 HTML 中用点而不是逗号显示十进制数字 在 Google Chrome 中输入标签类型 =“数字”

[英]How to display decimal numbers with dot instead of comma in HTML Input tag type=“number” in Google Chrome

I would like to know how can I use .我想知道如何使用. to separate decimals in a HTML Input tag type="number" instead of , using Google Chrome (Version 89.0.4389.82) MacOs Catalina.在 HTML 输入标签type="number"中分隔小数,而不是使用 Google Chrome(版本 89.0.4389.82)MacOs Catalina。

So far from what I've tried Firefox is the only browser which splits decimal numbers with .到目前为止,我已经尝试过Firefox是唯一一个使用.

Does anyone faced this issue?有人遇到过这个问题吗? Any ideas how to fix it?任何想法如何解决它?

 <input type="number" value="1.5" step="0.1" min="0" />

Browser decimal separators differ according to the region.浏览器小数点分隔符因地区而异。 I think your issue is parsing the value for different regions(, and.)我认为您的问题是解析不同区域的值(和。)

Use valueAsNumber to extract the value使用valueAsNumber提取值

valueAsNumber (MDN Documentation) valueAsNumber (MDN 文档)

double: Returns the value of the element, interpreted as one of the following, in order: double:按顺序返回元素的值,解释为下列之一:

  • A time value时间价值
  • A number一个号码
  • NaN if conversion is impossible如果无法进行转换,则为 NaN

 window.addEventListener('DOMContentLoaded', (event) => { const ageInput = document.querySelector('#age'); console.log('Age:', ageInput.valueAsNumber); console.log('Type of Age:', typeof ageInput.valueAsNumber); });
 <input id="age" type="number" value="1.5" step="0.1" min="0" pattern="[0-9]+([,\.][0-9]+)?" />

Also added pattern for sake of completeness.为了完整起见,还添加了模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM