douluchuo0801 2012-08-29 04: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 04:37
    关注

    You can do as follows

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部