Solved

Shorten variable to 100th decimal place

Posted on 2011-09-05
3
285 Views
Last Modified: 2012-05-12

I have a variable, $item_price, whose value is 55.0000

I want to shorten the number to the 100th place. I was trying to do the following but do not get output when I echo $item_price2. I wonder if I need to convert this value from a string to a number?? Thoughts?


$item_price2 = number_format($item_price, 2, '.', '');
0
Comment
Question by:pixelscape
3 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 36485558
"I want to shorten the number to the 100th place...."

Do you really want 100 decimal places? Or do you mean 2 decimals? The code you have posted should work for 2 decimals. What happens when you run the script?
0
 
LVL 24

Accepted Solution

by:
mankowitz earned 500 total points
ID: 36485711
if $item_price is a string, then you have to convert it into a float first,

$item_price2 = number_format(floatval($item_price), 2, '.', '');

Open in new window

0
 

Author Comment

by:pixelscape
ID: 36485799
Thanks mankowitz. That was it.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question