473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Using number_format without changing decimal

Hi everyone,

So, I'm running data through an array, and having a little difficulty
in using the number_format function. You see, I'm trying to get a
comma to appear for the thousandths place, however, whenever i use the
number_format function, it also forces rounding of the decimal.

Does anyone know how I can place commas in the thousandths place
WITHOUT doing any decimal rounding? I have some datapoints with two
decimal places, some with four, and others with none. I don't want to
have the function affect any of these decimal places.

Any suggestions?

-Tencip

Sep 12 '05 #1
2 2763
tencip wrote:
So, I'm running data through an array, and having a little difficulty
in using the number_format function. You see, I'm trying to get a
comma to appear for the thousandths place, however, whenever i use the
number_format function, it also forces rounding of the decimal.

Does anyone know how I can place commas in the thousandths place
WITHOUT doing any decimal rounding? I have some datapoints with two
decimal places, some with four, and others with none. I don't want to
have the function affect any of these decimal places.

Any suggestions?


preg_replace('/0+$/','',number_format($number,4,'.',','))
Sep 12 '05 #2
On 11 Sep 2005 18:36:09 -0700, "tencip" <te****@yahoo.com> wrote:
So, I'm running data through an array, and having a little difficulty
in using the number_format function. You see, I'm trying to get a
comma to appear for the thousandths place, however, whenever i use the
number_format function, it also forces rounding of the decimal.

Does anyone know how I can place commas in the thousandths place
WITHOUT doing any decimal rounding? I have some datapoints with two
decimal places, some with four, and others with none. I don't want to
have the function affect any of these decimal places.


This is a bit gruesome but it's the first thing that came to mind:

<?php
function number_format_keep_decimals($num)
{
if ($point = strrpos($num, '.'))
{
return number_format($num, strlen($num) - $point - 1);
}
else
{
return number_format($num);
}
}

$nums = array(1000.2, 2000.34, 5000.678, 9000);

foreach ($nums as $num)
{
print $num . ' -> ' .number_format_keep_decimals($num);
print "<br>";
}
?>

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Sep 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Dick Moores | last post by:
I need to figure out how to compute pi to base 12, to as many digits as possible. I found this reference, <http://mathworld.wolfram.com/Base.html>, but I really don't understand it well enough....
1
by: Andrés Giraldo | last post by:
Hi! I'm showing decimal values in a datagrid, but the decimal point shown is the "," and the correct is the ".". The datagrid is filled with a dataset, the dataset is filled with a SQL Server...
12
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a...
1
by: Alec Christie | last post by:
Hi All, I currently have 3 tables, Clients, Events and Event Status as follows: Clients: ClientID, Name, Address Events: EventID, Name, Date Event Status: StatusID, EventID, ClientID,...
7
by: _ed_ | last post by:
I'd like to build a class or struct composed of pointers to variables. Does this require dropping into an 'unsafe' block, or is there a trick? .... int value1 = 1234; bool value2 = false;...
3
by: Paul | last post by:
I have been trying to code a callback in vb.net using the delegate method. I have successfully compiled the program and run it but after it accessess the copyfileex winapi method and then calls the...
7
by: munnaj | last post by:
I have used the following code for submitting the values to paypal. but it is not working for firefox. Please give me a suggestion. // implied -- pbmods echo ' <script language="javascript"> ...
1
by: whitey | last post by:
Hi All, How do i define the number_format function to display the currency. it is showing dollars, and i want pounds thanks
17
TheServant
by: TheServant | last post by:
I have realized something which is going to be a headache in the rest of my current project. I don't know how I have not come across this before but let me show you an example: $x = number_format(...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.