1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

need PHP code to remove 699 from 699.0143

Discussion in 'PHP' started by please, Oct 13, 2008.

  1. #1
    Hello everyone!
    I use PHP and need to remove the integer from the scene. I have 699.0143 and would like to work with .0143 Please@helpmeOK.com
    ps: should also work on 8999.123:)
     
    please, Oct 13, 2008 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Try this

    
    <?php
    
    function removeInt($number) {
      if (!is_numeric($number)) {
        return false;
      } else {
          if (!strstr($number,'.')) {
          $number = number_format($number,1,'.', '');
          }
        $number = preg_replace('/([0-9]){1,}([.]){1}([0-9]*)/','$2$3',$number);
        
        return $number;
      }
    }
    
    // usage
    $number = 699.0143;
    $number2 = 8999.123;
    $number3 = 833.235534;
    $number4 = 89;
    print removeInt($number).'<br>'; // output .0143
    print removeInt($number2).'<br>'; // output .123
    print removeInt($number3).'<br>'; // output .235534
    print removeInt($number4).'<br>'; // output .0
    ?>
    
    PHP:
     
    ads2help, Oct 13, 2008 IP
    qprojects likes this.
  3. please

    please Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It took me a while to figure it out, but I got it to work and its just what the doctor ordered. Thanks you very much.:D
     
    please, Oct 14, 2008 IP
  4. clades

    clades Peon

    Messages:
    579
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    lol thats a big function just to do this:

    str_replace(intval($number),'',$number);

    :p

    Like einstein said: you write 100 pages of formulas just to say what i say in 6 characters
     
    clades, Oct 14, 2008 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    Unfortunately this suffers both a rounding bug & a potential collision if the int portions sequence is present in the float portion.

    I thought about using substr and fmod for a one-liner too, but it too suffers from a rounding bug.

     
    joebert, Oct 14, 2008 IP
  6. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Considering a variable is before all a string, you can also simply do:

    substr($number, strpos($number, "."), strlen($number)-strpos($number, "."));
    PHP:
    Of course, this only works if the decimal point is present.

    Note that using regular expressions turns you code quite not readable. Always think about the programmers that comes after you.
     
    webrickco, Oct 14, 2008 IP
  7. effektz

    effektz Active Member

    Messages:
    474
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #7
    $new = explode(".", $number); $new = $new[1];
     
    effektz, Oct 14, 2008 IP
  8. happpy

    happpy Well-Known Member

    Messages:
    926
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    120
    #8
    word. .
     
    happpy, Oct 14, 2008 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #9
    Wow. Talk about overthought answers. Converting to strings, using explode, regexp parsing... for a bloody floating point operation?!? Mein gott.... and people wonder why I say the majority of php programmers couldn't code their way out of a piss soaked paper bag with a hole in the bottom. That every single answer so far is treating it as a string is just mind-blowing to me given how STUPID that is since we're talking about a NUMBER. (and numeric processing is generally multiple orders of magnitude faster than string operations)
    function removeInt($inFloat) {
    	return $inFloat-floor($inFloat);
    }
    Code (markup):
    It's a float. Subtract the floor from the original. Basic ****** math people...
     
    deathshadow, Oct 14, 2008 IP
    AnonymousUser likes this.
  10. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #10
    lol. why so angry?
    Thanks btw. for floor().
     
    ads2help, Oct 14, 2008 IP
  11. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #11

    Converting to string? What are you talking about? In Php variables ARE strings!!!
    Well, we could have done this your way, it was simpler. But he said he wanted, for example, .178 from 69.178 and not 0.178, and that is what your function returns. Slight difference. Read the question before getting upset.
     
    webrickco, Oct 14, 2008 IP
  12. AnonymousUser

    AnonymousUser Peon

    Messages:
    593
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thanks for that, Didnt know the floor function, Rep!
     
    AnonymousUser, Oct 14, 2008 IP
  13. kaene

    kaene Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Is there any good tuto to learn how to use preg_replace, regular expressions are so powerful, but difficult .... thanks!
     
    kaene, Oct 15, 2008 IP
  14. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #14
    Except for the integers, booleans, objects etc.
     
    blueparukia, Oct 15, 2008 IP
  15. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #15
    Floats in particular. As BP implied you CAN use typecasting and php DOES have types. It just uses LOOSE typecasting converting as needed on the fly between types.

    Because if all variables were just strings, and we set $a to 3, the operation $a+=1 should either cause a syntax error or return 31. One of the whole REASONS php uses '.' instead of '+' on strings is so it can quickly figure out you want a string operation without explictly converting it to string.

    Use a mathematical operator, you get the appropriate mathematical type - be it integer or float... and computers handle mathematical operations dozens if not hundreds of times faster than they can handle strings. Strings are slow - abysmally and painfully slow. (see why XML as a data storage medium is made of /FAIL/)
     
    deathshadow, Oct 15, 2008 IP
  16. clades

    clades Peon

    Messages:
    579
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Hmm...exists a probability (extremely low but not zero) that the number vanishes from db and goes to mars, i think we should take that in count and parse it properly :D

    Basically everything you need is what i proposed as long you validade the number from user input, the rest is just quantic phisics that you dont need to understand to be able to use them...
     
    clades, Oct 15, 2008 IP
  17. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #17
    Clades - I assume english is not your primary language? That was vewy moist goodry.
     
    deathshadow, Oct 15, 2008 IP
  18. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #18
    It's called attention to detail, you should try it some time. :D

    The OPs example output didn't include a leading zero (0.1234), floating point operations are going to return a value with that leading zero.

    Programmers are only as good as their input. ;)
     
    joebert, Oct 15, 2008 IP
    webrickco likes this.
  19. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #19
    His English is good enough to be understood by the most. Why not making an effort?

    Your code is as good as anyone's else and comply with what is expected. In fact the only code presented here that doesn't is deathshadow's, that criticizes everybody's answer. Very arrogant and rude. Certainly not the proper way to behave in a forum. His code would return 0.123 over 15.123. The expected answer should have been .123. Read the thread before giving an answer! It's really not the same (except for a calculator maybe).

    The best code presented here is certainly ads2help's. It gives the correct result, deals with exceptions, it's readable and comply with most inputs.
     
    webrickco, Oct 15, 2008 IP
  20. imphpguru

    imphpguru Active Member

    Messages:
    439
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #20
    eekks... could say that was long... the first code..How about this

    <?php

    $str = "234.232";
    $pieces = split(".",$str);
    $newstr = ".".$pieces[count($str) - 1]; // should give you .232

    ?>

    Hope this makes it easy :).

    Thanks

    imphpguru

    ?>
     
    imphpguru, Oct 15, 2008 IP