Table of Contents
问题内容
解决方法
Home Java How to check the signed decimal used in a string number

How to check the signed decimal used in a string number

Feb 22, 2024 pm 01:00 PM

php小编苹果带来的java问答系列,本期讨论如何检查字符串数字中使用的符号小数。在Java中,我们经常需要对输入的字符串进行验证和处理,特别是涉及数字和小数点的情况。本文将介绍一些方法和技巧,帮助您轻松检查字符串数字中所使用的符号和小数点,确保数据的准确性和完整性。

问题内容

如何检查字符串数字是否有小数点或逗号

示例:

我已经尝试过这个,但不起作用

String strNumber="1,431,936.00"; //decimal sign uses a dot
  
String strNumber2="1.431.936,00"; //decimal sign using commas
  
if(strNumber.matches("^[0-9]*\\.?[0-9]*")){
    System.out.println("sign decimal used dot ");
}
   
if(strNumber2.matches("^[0-9]*\\,?[0-9]*")){
     System.out.println("sign decimal used commas ");
}
Copy after login

谢谢

使用匹配项进行检查

解决方法

您可以使用:

\d([.,]?\d)*
Copy after login

你可以看到它正在运行here.

它将允许您在两个数字之间嵌入单个逗号或点,但不能在末尾或两个数字在一起嵌入。如果你不介意的话,那么

[\d,.]*
Copy after login

更容易。任何点、数字或逗号序列都将作为一个组进行匹配。

如果您只想在逗号之前分隔三个数字组,而之后不分隔任何组,则

([1-9]\d{0,2}(,\d{3})*|0)\.\d*|([1-9]\d{0,2}(,\d{3})*|0)?\.\d+
Copy after login

查看here

The above is the detailed content of How to check the signed decimal used in a string number. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)