android parsing negative number strings

前端 未结 3 2121
有刺的猬
有刺的猬 2021-01-14 13:51

How to parse negative number strings?

strings may be of the form:

-123.23  
(123.23)  
123.23-

is there a class that will convert a

3条回答
  •  庸人自扰
    2021-01-14 14:06

    "-123.23" can be parsed with Float.valueOf("-123.23"); or Double.valueOf("-123.23");

    "(123.23)" and "123.23-" are invalid and need to be improved/parsed by yourself. You could go through the string step by step and check if it is a number, if so add it to a new string. If you find a - at the beginning or the end, make it than negative...

提交回复
热议问题