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
Solved

format a number

Posted on 2014-12-11
4
93 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 count occurrences of each item in an array.

856 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