Wednesday 15 May 2013

php - Remove trailing zeros from a string -


I want to zoom back to zero according to the following rules:

  • input number is always 4 The decimal place will be
  • The output number should always be between 2 and 4 decimal
  • If the last two digits after the decimal is zero, then remove both of them < Li> If the last digit is a zero after the decimal, then delete it

    Example:

    • 1.0000 - & gt; 1.00
    • 1.4000 - & gt; 1.40
    • 1.4100 - & gt; 1.41
    • 1.4130 - & gt; 1.413
    • 1.4136 - & gt; I have considered the following:
        if (substr ($ number, -2) == '00') return substagram ($ number, 0, 4); If (substr ($ number, -1) == '0') return substorage ($ number, 0, 5); Return number $;   

      Is there any better way to do this?

      I think this should work:

        return preg_replace ('/ 0 {1,2} $ /', '', $ number); $ String = array ('1.4000', '1.4100', '1.4130', '1.4136', '1.4001', '1.0041'); Forex currency ($ wire as $ number) {resonant "$ number -> Preg_replace ('/ 0 {0}} $ /', '', $ number). "\ N"; }   

      produces:

        1.4000 - & gt; 1.40 1.4100 - & gt; 1.41 1.4130 - & gt; 1.413 1.4136 - & gt; 1.4136 1.4001 - & gt; 1.4001 1.0041 - & gt; 1.0041    

No comments:

Post a Comment