triphis Posted April 3, 2007 Share Posted April 3, 2007 Hello I have tried to understand this function, but I just do NOT get it. I have tried to look at the copious "format phone numbers" scripts available, but I can't seem to change it to work for what I need. I actually need two forms: First, I have a number like this: 3032 stored in a DB, and I need to format it 30 x 32 (pant sizes lol). Also, a bit more complicated, I have either 325, or 32500 stored in a DB, and I need it to come out $325.00 This is for a little store website I am writing. Nothing in this store in less than $10 or more than $999, so basically, the first 2 or 3 digits will be the dollars. If the number is 4 digits long (4595) I need it to know to do $45.95 Three digits long would need .00 ADDED (because nothing would be $X.XX) 5 digits would obviously be XXX.XX and two digits would have .00 ADDED Thank you, and if you can even link me to a good resource for this, that would be GREAT! Link to comment https://forums.phpfreaks.com/topic/45367-preg_replace-string-formatting-help/ Share on other sites More sharing options...
per1os Posted April 3, 2007 Share Posted April 3, 2007 $num = 3032; $len = substr($num, 0, 2); $wid = substr($num, 2,2); print $len . " " . $wid; $price = 4595; $price = number_format($price, 2, "."); print $price www.php.net/substr www.php.net/number_format Link to comment https://forums.phpfreaks.com/topic/45367-preg_replace-string-formatting-help/#findComment-220286 Share on other sites More sharing options...
trq Posted April 3, 2007 Share Posted April 3, 2007 For your first question... take a look at substr. As for your second... take a look at number_format. Link to comment https://forums.phpfreaks.com/topic/45367-preg_replace-string-formatting-help/#findComment-220288 Share on other sites More sharing options...
dough boy Posted April 3, 2007 Share Posted April 3, 2007 I was actually going to ask why you chose to store your data this way instead of as a decimal field for the money, and a varchar field for the "size"? Link to comment https://forums.phpfreaks.com/topic/45367-preg_replace-string-formatting-help/#findComment-220289 Share on other sites More sharing options...
triphis Posted April 4, 2007 Author Share Posted April 4, 2007 Thank you frost and thorp i will have to edit the price one a bit so that it adds the .00 where necessary, but that works just fine, thanks I didn't expect it to be so simple. obviously I am new to this dough boy, I was not aware that there is a decimal field o.O not "int" is it? and I do actually have the size in a varchar field. I guess I don't really know why I am making it more difficult on myself... I am clearly going to have to rethink my DB structure LOL Link to comment https://forums.phpfreaks.com/topic/45367-preg_replace-string-formatting-help/#findComment-220955 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.