abap判断包含字符当中包含小数点,如何检查字符串只包含数字和小数点?

For example I want to check that my when I split my string that the first part only contains numbers and decimal points.

I have done the following

String[] s1 = {"0.12.13.14-00000001-00000", "0.12.13.14-00000002-00000"};

String[] parts_s1 = s1.split("-");

System.out.println(parts_s1[0]);

if(parts_s1[0].matches("[0-9]")

But thats only checking for numbers and not decimals. How can I also check for decimals in this? For example I want to check for 0.12.13.14 that it works and something like 0.12.13.14x will not work.

解决方案

Add the dot character in the regex as follows:

if(parts_s1[0].matches("[0-9.]*")) { // match a string containing digits or dots

The * is to allow multiple digits/decimal points.

In case at least one digit/decimal point is required, replace * with + for one or more occurrences.

EDIT:

In case the regex needs to match (positive) decimal numbers (not just arbitrary sequences of digits and decimal points), a better pattern would be:

if(parts_s1[0].matches("\\d*\\.?\\d+")) { // match a decimal number

Note that \\d is equivalent to [0-9].

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ABAP编程中,可以使用特定的格式指令来使数量整数不显示小数点。在输出或显示数量整数的时候,可以使用以下两种方法: 方法一:使用F格式指令 在输出或显示数量整数的地方,可以使用ABAP的F格式指令来指定输出格式。在F格式指令中,可以使用“e”选项来指定只显示整数部分,忽略小数部分。例如,可以使用以下代码来实现数量整数不显示小数点: DATA: lv_quantity TYPE p DECIMALS 2 VALUE '123.45'. WRITE lv_quantity TO lv_output USING EDIT MASK 'FM999,999,999.99'. 在上述代码中,将数量整数赋值给变量lv_quantity后,通过WRITE语句输出到lv_output变量中,并使用EDIT MASK ‘FM999,999,999.99’指令来格式化输出。在F格式指令中,使用FM选项来指定忽略小数部分,并使用逗号分隔符来增加可读性。 方法二:使用转换函数 在输出或显示数量整数的地方,可以使用ABAP的转换函数来将数量整数格式化为字符串,并使用相应的转换选项来忽略小数部分。例如,可以使用以下代码来实现数量整数不显示小数点: DATA: lv_quantity TYPE p DECIMALS 2 VALUE '123.45'. DATA(lv_output) = CONV string( lv_quantity ). 在上述代码中,将数量整数赋值给变量lv_quantity后,使用CONV string函数将其转换为字符串,并赋值给lv_output变量。转换为字符串后,小数部分将自动被忽略,只显示整数部分。 以上两种方法都可以实现在ABAP编程中使数量整数不显示小数点。根据实际情况选择适合的方法即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值