0

I have a Custom Post Type with a Metabox and an input Text field for adding "Price" to the posts. Now I would like to display the input value which can be like 49 or 59 like 49.00 or 59.00 so far I have this code to output the meta as:

$meta = get_post_custom($post->ID);
echo '<h5> $'.$meta['price'][0].'/h5';

but this just return the value as $49 or $59. Can you please let know how I can make it to be render as $49.00 ?

Thanks

1

WordPress core has number_format_i18n() function, which allows to set number of decimal digits and apploes appropriate formatting in line with site's locale.

However note that it is not specific to money and there can be more peculiar requirements to handle such.

1

I would recommend using the php core function money_format()

the code should look something like this:

$meta = get_post_custom($post->ID); // value=123456

setlocale(LC_MONETARY, 'en_US');

echo money_format('%i', $meta) . "\n"; // USD 1,234.56

Cheers

0

You should use the php number_format function which does what you exactly want.

The second parameter of number_format sets how many decimal point you want after the number.

Example:

<?php
echo number_format(123, 2); //123.00
echo number_format(123, 4); //123.0000

So with your code it would be:

$meta = get_post_custom($post->ID);
echo '<h5> $' . number_format($meta['price'][0], 2) . '</h5>';
0

Rahil Wazir's answer can do the job but I think is really better use the money_format function from PHP*.

*Remember that this site is for Wordpress specific questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.