SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Limit length of number irrespect of decimal places

    I need to return a max of 8 digits to any number (not inc decimal point if necessary), but also want a max of 4 decimal places if the calculation provides a results with more than 4 dec places. This is because i have a max space of 8 characters + dec place.

    eg

    10.0908 (fewer than 8 digits so full 4 decimal places)
    1908978.5 (max digits so could only return 1 dec place)
    222.0192 (fewer than 8 digits so full 4 dec places shown)
    1.19 (this is full answer so no changes required as !=> 8 digits & dec places<4 any way)

    Hope this makes sense!!

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the result?

    123456.7555

  3. #3
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For that matter, what would the result of 1234567890 be? Do non-fractional numbers keep their whole value?

  4. #4
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually seems ok to squeeze any 9 characters in, so partly solved by this:

    $charLimit = 9;
    if (strlen($answer) <= $charLimit)
    {
    $answer_value = "$answer";
    } else {
    $answer_value = substr($answer,0,9);
    }

    But this leaves me with sometimes having a decimal place as last character. Whats the best way to determine if the 9th character is the decimal point and if so remove it?

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rtrim()

  6. #6
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks!


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
  •