Link to home
Start Free TrialLog in
Avatar of bertmn
bertmn

asked on

PHP regex for to add zero to before or after included number.

Hi,

I need a regex that will add beginning or trailing zero to a set of numbers.

Sample numbers
6.76 will turn into 0676
15.5 will turn into 1550
176.5 will turn into 17650

Basically the zeros are not stored in the data and the program that this data is going to needs those zeros.
If it's easier to leave the decimal point in on the regex that's fine I can use str_replace to remove it after the proper zeros have been added via the regex statement.

TIA
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

What is the rule we would use to decide whether the zero  belongs on the front or the rear of the numbers?

For example, why would we get 0676 instead of 6760?  This is a rule we would need to understand in order to answer your question.
Avatar of bertmn
bertmn

ASKER

The decimal point would be the reference, its dollars and change but always requires at least 4 numbers. So 6.76 turns into 0676 but 15.5 which is 15.50 would need to be 1550. Does that make sense? All the starting values have 1 decimal point in them but require two numbers after the decimal and before the decimal.
PHP has it's own function for setting the number of decimal places -- number_format. This function can also add commas if you want them. But it doesn't pad the left side and I'm assuming you need a minimum of four characters. Try:
$numbers = array(6.75, 15.5, 176.5);
foreach ($numbers as $num) {
	// set two decimal points and remove decimal
	$fnum = number_format($num,2,'','');
	// pad left to minimum of four characters
	while (strlen($fnum) < 4) { $fnum = '0'.$fnum; }
	echo $fnum."\n";
}

Open in new window

Result:
0675
1550
17650

Open in new window

Avatar of Olaf Doschke
Regex are good for pattern matching, not so good for achieving some pattern. It still puzzles me, why you insist on 4 places overall at least. The requirement would be clear and elegant, if you'd simply defined you need amounts in cents. Then simply multiply currency values by 100. The way you describe that sounds not only like someone played chinese whispers and the telephone game, but tried to maximise the complexity of the requirement. Even matching exactly that requirement would be currency amounts converted to cents and string formatted to minimum 4 places.

I can only imagine the requirement for left alignment, while the alignment would also work with right alignment of the cent amounts without any left side padding. And if the values should be padded for left alignment, they must be padded to max width, not 4.

Bye, Olaf.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bertmn

ASKER

I'm sure you are all wondering why I need this strange formatting. I am using php to take some excel data and add it to a macro. The macro formatting of all the samples I was provided use 4 numbers at minimum for the entry of these numbers but the excel data was missing the zeros I needed. To not have to edit the excel file prior to merging but staying consistent with the macro is why I needed the before and after zeros to make sure at minimum 4 numbers were entered. I hope that helps those that were curious why I needed this formatting. Thank you.