Solved

format a number

Posted on 2014-12-11
4
98 Views
Last Modified: 2014-12-11
ok, I have tried floor and number_format but each wants to round. I don't want to round the number.

Example: 28.955875 should be 28.95 I don't want 28.96

language is PHP.
0
Comment
Question by:Robert Saylor
[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
  • 2
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40495370
There is never a good, generalized solution, when you want PHP to give you what amounts to the "wrong" answer with numbers.  This works, given the test data, but I would not want to depend on it.
http://iconoun.com/demo/temp_robert_saylor.php

<?php // demo/temp_robert_saylor.php
error_reporting(E_ALL);

/**
 * SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28579407.html
 * REF: http://php.net/manual/en/function.floor.php#88197
 *
 * Example: 28.955875 should be 28.95 I don't want 28.96
 */

// RAW DATA
$float = 28.955875;

// FORCE TYPECASTING TO STRING AND TRUNCATE THE DATA
$numbr = substr("$float",0,5);

var_dump($float, $numbr);

Open in new window

0
 
LVL 58

Accepted Solution

by:
Gary earned 250 total points
ID: 40495398
No matter the number, even if it's 128.955875 or 3428.955875 and so on this will just format to 2 unrounded decimal places and not just grab the first 5 characters.

<?php
$thenumber = 28.955875;
echo number_format(floor($thenumber *100)/100, 2);

Open in new window

0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40495413
I recommend you use Gary's solution, so long as you understand what you're getting (from a financial standpoint).  

Best regards, ~Ray
0
 
LVL 7

Author Closing Comment

by:Robert Saylor
ID: 40495428
Thanks Gary and Ray! Glad you guys are always online :) And yep this was for calculating dollars.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

624 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