Link to home
Start Free TrialLog in
Avatar of Robert Saylor
Robert SaylorFlag for United States of America

asked on

format a number

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.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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

ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robert Saylor

ASKER

Thanks Gary and Ray! Glad you guys are always online :) And yep this was for calculating dollars.