Convert Price Type

Discussion in 'PHP' started by adamjblakey, Jul 15, 2009.

  1. #1
    Hi,

    I currently have on my website the price type set to e.g. 4.20 i want to convert this to e.g. 0420 is there a function that can do this at all?

    Cheers,
    Adam
     
    adamjblakey, Jul 15, 2009 IP
  2. mdrobiul

    mdrobiul Peon

    Messages:
    186
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i can't exactly write. it's possible. i read in where printf() function was discussed and see details in related chapters..
     
    mdrobiul, Jul 15, 2009 IP
  3. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Be more speicifc, do you want to convert all of your prices, and when is a leading/trailing zero acceptable ?
     
    wd_2k6, Jul 15, 2009 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Yes i want to convert all my prices to this format so e.g. if the price is e.g. £14.95 it will be 1495 and if the price was £1.95 it would be 0195.
     
    adamjblakey, Jul 15, 2009 IP
  5. elitasson

    elitasson Member

    Messages:
    49
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #5
    function price($price) {
    
    	str_replace('.', '', $price);
    
    	$count = strlen($price);
    	
    	if($count == 2) {
    
    		$price = '00' . $price;
    	}
    	elseif($count == 3) {
    
    		$price = '0' . $price;
    	}
    
    	return $price;
    }
    PHP:
    Should work.
     
    elitasson, Jul 15, 2009 IP
    newgenservices likes this.
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Thank you for the reply but that does not seem to have done anything.

    Any Ideas?
     
    adamjblakey, Jul 15, 2009 IP
  7. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    there is a small mistake, maybe a typo when elitasson was writing the function fast.
    happens all the time :)

    this:
    str_replace('.', '', $price);
    PHP:
    should be
    $price = str_replace('.', '', $price);
    PHP:
    now
    echo price("14.95");
    PHP:
    will print
    1495
    and
    echo price("01.49");
    PHP:
    will print
    0149
     
    zeronese, Jul 15, 2009 IP
  8. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Thanks a lot :)
     
    adamjblakey, Jul 16, 2009 IP
  9. elitasson

    elitasson Member

    Messages:
    49
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #9
    oh, thanks for correcting me :D
     
    elitasson, Jul 16, 2009 IP
  10. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    no problem elitasson, just test the code before going live :)
     
    zeronese, Jul 16, 2009 IP