Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

format a number

Posted on 2014-12-11
4
Medium Priority
?
106 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 111

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 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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

Independent Software Vendors: 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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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 …
Suggested Courses

783 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