Solved

format a number

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

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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 6

Author Closing Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Router for PHP reqeusts 12 29
Php  POSTGRES  "  IN SIDE "  " 4 37
Mysqli Query 5 40
How to validate 2 forms at once 8 24
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…
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 is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now