Forum Moderators: coopster

Message Too Old, No Replies

Want Commas In Your Numbers?

Use the function I made

         

brendan3eb

1:34 am on Jul 9, 2005 (gmt 0)

10+ Year Member



Just made 2 functions for putting commas into your numbers. Also, these two functions with a little bit of syntax changes will work on TI-83 calculators, making it easier to awnser a question without looking like your using your calculator :).
<?php
function is_divisible ($num, $numtwo) {
$divide = $num % $numtwo;
if($divide!= 0)
{
return FALSE;
}
else
{
if($num == 0)
{
return false;
}
else
{
return true;
}
}
}
function limitChars ($str)
{
$chars = strlen($str);
$str = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
$k=0;
$remainder = $chars % 3;
$charsleft = $chars;
while($k < $chars)
{
if(is_divisible($charsleft, 3))
{
if($k!= 0)
{
$quotient = $chars - $k;
}
else
{
$quotient = 0;
}
if(is_divisible($quotient, 3))
{
$particle = $particle.",".$str[$k];
}
else
{
$particle = $particle.$str[$k];
}
}
else
{
$charsleft--;
if($k == $remainder)
{
$particle = $particle.",".$str[$k];
}
else
{
$particle = $particle.$str[$k];
}
}
$k++;
}
return $particle;
}
echo limitChars(12345);
?>

brendan3eb

1:35 am on Jul 9, 2005 (gmt 0)

10+ Year Member



I wasn't sure how I was going to do this thats why some variable names like $quotient are in there when they should be difference, either way you still get your formatted number in the end :P

sonjay

4:54 am on Jul 9, 2005 (gmt 0)

10+ Year Member



Is there some reason you can't use php's built-in number_format() [us3.php.net] function?

brendan3eb

5:03 am on Jul 9, 2005 (gmt 0)

10+ Year Member



dope! I searched, but didn't find that. It was fun making the two functions anyway :P

sonjay

2:41 pm on Jul 9, 2005 (gmt 0)

10+ Year Member



I think we've all done that! There are so many built-in php functions that no one can possibly memorize them all. And if you don't already know the name the function, how do you search for it?

Good attitude on your part -- you had fun, and that kind of thing is good practice, even if the function already exists.

larryhatch

3:11 pm on Jul 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One little thing bothers me.

In the USA we use commas to denote thousands, and then million . billions ..

In Western Europe, they use the decimal point (dot) the same way we use the comma.
For example:
In Germany or France, one might write "I made 11.275 Euros last month.:
They guy did well in Euros, but that's only 14 bucks in the USA.
Quick now! Is the following number less than a billion or more than a billion?

1000000000

I can't tell myself, full of beer and having misplaced my glasses.
I had two cold glasses of bier, I probably left one or the other in the can.
The French and Germans would use commas like everybody else if they
weren't trying to save ink money. Don't get me started on the English. -Larry

mogenshoj

7:15 pm on Jul 9, 2005 (gmt 0)

10+ Year Member



well, in Denmark, which would also be western europe, we use:

11250 = 11.250

and

11,25 = 11 1/4

dcrombie

4:42 pm on Jul 11, 2005 (gmt 0)



You can 'tweak' the output to your local convention by using setlocale [php.net]. I'm not sure if this applies to number_format, but it definately works for money_format and for days of the week ;)

gliff

6:40 pm on Jul 11, 2005 (gmt 0)

10+ Year Member



There are so many built-in php functions that no one can possibly memorize them all. And if you don't already know the name the function, how do you search for it?

There's this wonderful site some of you may have heard of called Google [google.com] that will let you (get this) search for things on a website! Who knows how they plan to make any money on it, but it's great for searching the PHP documentation! ;)

(tongue firmly and "good-naturedly" planted in cheek)

jatar_k

6:53 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<sorry OT>
>> if you don't already know the name the function, how do you search for it

then you look through the manual [php.net] for what type of data you are dealing with and read through the functions available until you find something that might work.

I know it takes a lot of time but it will help you a lot in the future as to understanding all of the things you can do. You may just have that "I think I remember seeing a function for that..." moment. ;)

I often read through the main pages for strings, arrays, math, mysql, among many others. It helps figure out what new functions might be available in any given release, as well as giving you a look at functions you may not have seen before because they are not often used.

I highly recommend that people spend some of their free time doing this, it shortens the learning curve more than you could ever imagine.
</OT>

mincklerstraat

12:01 am on Jul 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



brendan3eb, you're in good company, I've written functions for number_format() as well until I bothered reading the manual more carefully. Jatar's advice is excellent here.

brendan3eb

2:04 am on Jul 12, 2005 (gmt 0)

10+ Year Member



thanks for all the advice :P

There's this wonderful site some of you may have heard of called Google that will let you (get this) search for things on a website! Who knows how they plan to make any money on it, but it's great for searching the PHP documentation! ;)

(tongue firmly and "good-naturedly" planted in cheek)

I usually do use google when looking for functions, and I'll usually google "string functions" or whatever category the function would be in. Either that or I'll find a function that is in that category if I don't know the name of the category and then look through the similar functions list on the left. However, there are times when I can't find the function I'm looking for or I just skip over the function I'm looking for (especially when I'm working late at night or after diving practice :P).

Like sonjay said, I've learned from making the function and I've now had the importance of the php manual to saving me time emphasized. I also have a function for my calculator to help me get some sleep in class :)