Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Currency place holder

  1. #1
    Join Date
    Nov 2006
    Location
    New York
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Currency place holder

    ok, hopefully this will be my last question for day...

    If there a way to make a total come out with cents places held?
    Code:
    	$price = .50 * $o[count];
    	print "$o[count] at $.50    -   $price";
    so even if there is only 1 $price shows up as .5

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by rocg23 View Post
    If there a way to make a total come out with cents places held?
    Do you mean to add trailing zeros? That is, "1." should be "1.00" and "0.3" should be "0.30".

    Mike

  3. #3
    Join Date
    Nov 2006
    Location
    New York
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Yes, that is exactly what I am looking for...thanks

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    This is likely not the 'official' way, but here's an option and fairly simple...not sure if there's a faster/more efficient way (in server time):
    PHP Code:
    <?
    //$x is the value to be converted to "money" format
    $x $x*100;
    $x round($x);
    $x substr($x,0,strlen($x)-2).".".substr($x,strlen($x)-2);
    //next line optional:
    $x "$".$x;
    ?>
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You should escape that dollar sign. Also, don't use short tags (<?, <?=?>, &c.) when you don't know the server, since it might not have them enabled.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Oh, weird. The <? was a typo

    escape fixed... just in single quotes instead due to that earier discussion...

    Updated code:
    PHP Code:
    <?php
    //$x is the value to be converted to "money" format
    $x $x*100;
    $x round($x);
    $x substr($x,0,strlen($x)-2).'.'.substr($x,strlen($x)-2);
    //next line optional:
    $x '$'.$x;
    ?>

    Twey, does that seem like the logical solution for the problem? It's a pretty simple answer. Unless there's a specific function for this, I'd say it makes sense.
    Would round($x,2) work? I think it would round correctly, but leave off trailing 0s... is that right?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's the way I'd do it, if money_format() weren't available.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    One should keep in mind that monetary calculations are best performed using scaled integers: floating point data types cannot represent all fractions exactly, unlike integers.

    PHP Code:
    function formatMoney($number$currency '$') {
        
    $string str_pad(round($number), 3'0'STR_PAD_LEFT);

        return 
    $currency substr($string0, -2) . '.' substr($string, -2);

    If the first argument is a floating point number, it will rounded to an integer. For example, 1500.5 would be formatted as "$15.01".

    Mike

  9. #9
    Join Date
    Nov 2006
    Location
    New York
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Worked like a charm, thanks...now one of these days I have to actually learn what this all means.

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    mike, that assumes that the input will be given in cents. I thought the question was related to something already in dollars, with the cents as the decimal.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •