Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how do I do this with numbers in a string?

Posted on 2004-10-24
8
Medium Priority
?
187 Views
Last Modified: 2008-07-03
I have a Float say 25.4812756

When I dump it out, I want to limit my output to say, 25.48.  how can I limit the number of decimal places that get outputted into a string?

Thanks!
0
Comment
Question by:will1383
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Expert Comment

by:BenMorel
ID: 12394789
Hello,

You should use printf (or sprintf in this example) for this purpose.

<?php
$float_number = 25.4812756;
$string = sprintf('%01.2f', $float_number);
echo $string; // 25.48
?>

See ya ;)
Ben
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
ID: 12394856
Use the round function, its what its designed for. printf is not needed here.

<?php
$var = 25.4812756;
echo round($var,2);
?>

http://us2.php.net/manual/en/function.round.php
0
 

Author Comment

by:will1383
ID: 12394883
Oh yea,

Just like C.  Doh!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:minichicken
ID: 12394907
Hi

You can also use number_format(); to do more formating to you output.

>>number_format ( float number, int decimals, string dec_point, string thousands_sep)
http://za2.php.net/number_format

e.g

$number = 1234.5678;

echo number_format($number, 2, '.', ',');

//outputs // 1,234.57

0
 
LVL 7

Expert Comment

by:BenMorel
ID: 12394926
I agree Diablo ;)

The difference is that round($n,$max) outputs $n with $max MAXIMUM decimals.
printf() here has a fixed number of decimals, which is better for printing a price, for example.
Depends on what it is used for!

Ben
0
 

Author Comment

by:will1383
ID: 12394931
oh man.

sorry about pulling the trigger with the accepted answer.  I didn't realize there was this many ways to handle number formatting in php.  Thank you ALL!!!
0
 
LVL 7

Expert Comment

by:BenMorel
ID: 12394969
No problem Will ;)
0
 
LVL 12

Expert Comment

by:minichicken
ID: 12394977
No Prob...

I answered the question too slow anyways..... :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

636 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