Solved

format a number

Posted on 2014-12-11
4
92 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
  • 2
4 Comments
 
LVL 109

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 109

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I remove all except the numbers?  PHP  Regex 3 29
PHP: Query an Sqlite3 Datatbase return in JSON 6 51
mysqli insert query problems 4 22
comma true 6 33
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

773 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