douluchuo0801 2012-08-29 12:31
浏览 111
已采纳

如何使用PHP替换括号并向数字添加减号

I have this variable- $number which may contain different types of number format.

Type1: $number=200,000.00 ; Type2: $number=(200,000.00) ; Type3: $number=(20.50) ;

If the number has brackets I want to remove the brackets and add a minus sign on its left side.

Example: If the number is $number=(200,000.00) ; I want to convert it to -200,000.00 or in case of $number=(20.50) ; I want to convert it to -20.50 but for $number=200,000.00 ; I want to keep it just the way it is.

I have tried this preg_replace('/[()^\d-]+/', '', $number); but its not working.

Could you please show me how to get this done?

Thanks :)

  • 写回答

3条回答 默认 最新

  • douchun3680 2012-08-29 12:37
    关注

    You can do as follows

    str_replace(array("(",")"),array("-",""),$number);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?