Steve Price Steve Price - 17 days ago 7
PHP Question

check to see if number ends in .00 using php

I'm working on something in zen cart and I don't want .00 to display on prices.
I thought I fixed the issue by doing

$price=number_format($special_price,2,'.','');


but zen cart has a function that adds the required currency symbol to the front or back of the number

$currencies->format($price)


The problem is that this function adds the .00 back onto the value!

The code for the function is

$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(zen_round($number * $rate, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];


If I duplicate that function so that I have $currencies->format2($price) and change it to

$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(zen_round($number * $rate, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['0'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];


then it will add the currency symbol without adding the decimal places back in. Of course, when you have a price such as 49.50 it is then rounding this up to 50

I did try

$cur_price=$currencies->format($special_price);
$price=str_replace(".00", "", (string)number_format ($cur_price, 2, ".", ""));


The thinking behind it being that I could apply the currencies symbol first and then remove the decimals if they were .00, but that resulted in a blank space where the price should be.

I either need to find a way to check if

$price


ends in .00 so i can conditionally call $currencies->format() or $currencies->format2(), or I need to modify the original function to not put the decimal in place if it's .00, but allow it at all other times.

Answer

Would

$price = substr($price, -3) == ".00" ? substr($price, 0, -3) : $price;

work?

Comments