rhog23
rhog23

Reputation: 321

How to remove ,00 or .00 from the back of a price?

How can I remove ,00 from the back of a price? Example: input1: 100.000,00 output1: 100.000

input2: 100.000.00 output2: 100.000

what I've tried is:

preg_replace("/[^(0-9)\.\,(0-9)]/", '', $text)

but it always return 100

Thanks for the help

Upvotes: 0

Views: 478

Answers (3)

Karlo Kokkak
Karlo Kokkak

Reputation: 3714

You can use number_format()

$number = '100.000,00';
echo number_format(floatval($number), 3, '.', '');

Output:

100.000 // same output for '100.000.00'

http://php.net/manual/en/function.number-format.php

Upvotes: 1

LF-DevJourney
LF-DevJourney

Reputation: 28524

You can use substr to remove the last three chars.

substr('100.000,00', 0, -3)

Upvotes: 0

Goma
Goma

Reputation: 1981

You can try with this pattern

preg_replace('/[\.,]0{2}$/', '', $text);

Notes:

  • [\.,] - Matches single period or comma
  • 0{2} - matches the character 0 exactly two times
  • $- assert position at the end of string

Upvotes: 1

Related Questions