bagisto / bagisto

Free and open source laravel eCommerce platform
https://bagisto.com
Open Software License 3.0
15.38k stars 2.21k forks source link

Remove decimal points from price #10305

Closed amirseyedian closed 1 week ago

amirseyedian commented 1 week ago

Describe the solution you'd like

core.php

public function currency($amount = 0)
{
    if (is_null($amount)) {
        $amount = 0;
    }

    return $this->formatPrice($this->convertPrice($amount));
}

public function formatPrice(?float $price, ?string $currencyCode = null): string
{
    if (is_null($price)) {
        $price = 0;
    }

    $currency = $currencyCode
        ? $this->getAllCurrencies()->where('code', $currencyCode)->first()
        : $this->getCurrentCurrency();

    return $this->formatCurrency($price);
}

public function formatCurrency(float $price): string
{
    $locale = app()->getLocale();
    $formatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL);
    $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 0); // No decimal points

    return $formatter->format($price);
}
devansh-webkul commented 1 week ago

We actually already have this set up for the custom formatter. 😊 You can find it here: CurrencyFormatter.php.

And as for the default formatting, it’s all handled automatically based on the locale, so there’s no extra work needed. Hope this helps, but please let me know if you need more details—I’m happy to assist!