Search code examples
javastringdecimalbigdecimal

Comma not found in string with .contains method


I need to find if a string containes a comma or a dot.
When I use the following code it is working only if I have a dot but the comma is not working. Why ?

value is type string.

For example :

If I have number like 200 and I wish to add to it .0 this is fine, but if I my number is like 200,53 it also adds .0 to it : 200,53.0
Why?

if (!value.contains(".") || !value.contains(",")) {
           value = value + ".0";
}

Solution

  • Shouldn't it be && ? Because, as I understand, you want to append .0 if the number doesn't contain BOTH . AND ,

    if (!value.contains(".") && !value.contains(",")) {
               value = value + ".0";
    }