• Hi Guys. does anyone know how to add a percentage to a custom field if its a number?

    So for example my code is;

    <?php echo get_post_meta($post->ID, ‘price’, true) ?>

    If the value of price is 100 and i want it to automatically add 20% so that the outcome is 120, how is this possible?

    Regards

Viewing 2 replies - 1 through 2 (of 2 total)
  • PHP is not a strongly typed language so you can just do this

    Verbose code

    <?php
      $percentage=0.2; //20%
      $originalValue=get_post_meta($post->ID, 'price', true);
      $adjustedValue=$originalValue * (1 + $percentage);
      echo $adjustedValue;
    ?>

    Short code if you don’t think you’ll need to reuse the percentage, the original value or adjusted value anywhere else;
    <?php echo get_post_meta($post->ID, 'price', true) * 1.2; ?>

    Thread Starter ringer

    (@ringer)

    Xephan!

    Thank you so much!

    It works a treat, I wish I could buy you a nice cold beer! Or cup of tea!

    Just one more question, how do i get it to round up or down the the nearest penny!

    At the mo it displays for example:

    £45.425

    Where as id like it to display £45.43!

    Regards

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Tricky Custom Field Question!’ is closed to new replies.