How to Remove a Zero From a Decimal in PHP

104 74
    • 1). Open your PHP source file in a text editor, such as Windows Notepad.

    • 2). Declare a variable and assign it a decimal value. For example, "$number = 1.200;."

    • 3). Check if the number is equal to zero first, so you don't remove the only zero and create an empty string. For example, "if ($number != 0)."

    • 4). Remove trailing zeros by using the "rtrim(string,character)" function. For example, "if ($number != 0) { $number = rtrim($number,'0'); }" changes the number from "1.200" to "1.2."

    • 5). Remove leading zeros by using the "ltrim(string,character)" function. For example, "$number = 0.35; if ($number != 0) { $number = ltrim($number,'0'); }" changes the number from "0.35" to ".35."

    • 6). Remove both leading and trailing zeros by using the "trim(string,character)" function. For example, "$number = 0.7100; if ($number != 0) { $number = trim($number,'0'); }" changes the number from "0.7100" to ".71."

    • 7). Save the PHP file.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.