473,503 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to stop variable rounding up after a division

I am trying to take an integer and divide it by 100 to give a decimal to
store in my database. The field in my mysql database is set to decimal(4,2)
My php is
$price=number_format($price/100,2);
Ive also tried
$price=number_format($price,2)/100;

but neither work it keeps rounding any value less than 100 to 1 whereas I
want it to store the value to two DP

eg

80 becomese 1 instead of 0.8
180 becomes 2 instead of 1.8

does anyone know how to do this?
Ian
Oct 7 '06 #1
4 3505
mantrid wrote:
I am trying to take an integer and divide it by 100 to give a decimal to
store in my database. The field in my mysql database is set to decimal(4,2)
My php is
$price=number_format($price/100,2);
Ive also tried
$price=number_format($price,2)/100;

but neither work it keeps rounding any value less than 100 to 1 whereas I
want it to store the value to two DP

eg

80 becomese 1 instead of 0.8
180 becomes 2 instead of 1.8

does anyone know how to do this?
Ian

AFAICS, number_format should deliver a string, which is what a
decimal(4,2) expects.

I wonder if you are failing to quote the string in your SQL? It would be
easier to tell if you posted the code.

On thing to try is to print out $price after the number_format, to check
whether the rounding happens then or later.

Colin
Oct 7 '06 #2
*** mantrid escribió/wrote (Sat, 07 Oct 2006 18:36:03 GMT):
$price=number_format($price/100,2);
$price=number_format($price,2)/100;
80 becomese 1 instead of 0.8
180 becomes 2 instead of 1.8
In my system, this code:

<?php

echo number_format(80/100, 2) . "\n";
echo number_format(80, 2)/100 . "\n";

?>

prints this output:

0.80
0.8

:-?
You can try forcing a cast to float, that works in other languages:

(float)$foo
100.0
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Oct 7 '06 #3
Yes that was it
,'$price', works in the sql
whereas
,$price,
does not
thanks

"Colin Fine" <ne**@kindness.demon.co.ukwrote in message
news:eg*******************@news.demon.co.uk...
mantrid wrote:
I am trying to take an integer and divide it by 100 to give a decimal to
store in my database. The field in my mysql database is set to
decimal(4,2)
My php is
$price=number_format($price/100,2);
Ive also tried
$price=number_format($price,2)/100;

but neither work it keeps rounding any value less than 100 to 1 whereas
I
want it to store the value to two DP

eg

80 becomese 1 instead of 0.8
180 becomes 2 instead of 1.8

does anyone know how to do this?
Ian
AFAICS, number_format should deliver a string, which is what a
decimal(4,2) expects.

I wonder if you are failing to quote the string in your SQL? It would be
easier to tell if you posted the code.

On thing to try is to print out $price after the number_format, to check
whether the rounding happens then or later.

Colin

Oct 8 '06 #4
Alvaro G. Vicario wrote:
*** mantrid escribió/wrote (Sat, 07 Oct 2006 18:36:03 GMT):
>$price=number_format($price/100,2);
$price=number_format($price,2)/100;
>80 becomese 1 instead of 0.8
180 becomes 2 instead of 1.8

In my system, this code:

<?php

echo number_format(80/100, 2) . "\n";
echo number_format(80, 2)/100 . "\n";

?>

prints this output:

0.80
0.8

:-?
You can try forcing a cast to float, that works in other languages:

(float)$foo
100.0

That makes sense. The second example is not formatting the fraction, so
it is printed in default format.

Colin
Oct 8 '06 #5

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

Similar topics

2
5183
by: engsol | last post by:
I'm using Python to parse a bunch of s/w test files and make csv files for later report generation by MS ACCESS....(my boss loves the quick turn-around compared to C). Each log file may contain one...
3
9372
by: Dalan | last post by:
Is there any code available to address currency rounding problems in Access 97? Apparently, selecting currency type table fields does not resolve the problem. For instance, in my form I have a...
20
15854
by: Daisy | last post by:
Something simple, I'm sure. My code: System.Windows.Forms.MessageBox.Show(((Int32)Math.Ceiling(1966 / 100)).ToString()); I'm expecting 20, but getting 19. What am I doing wrong? Thanks --
9
2670
by: itportal | last post by:
Hello, This will probably be a simple question. How can I round float/doubles to a specific digit after the comma/dot? For example we have float i = 1.234567. I want to round it to 1.234....
4
3854
by: Jackson Peebles | last post by:
Hey everyone! I'm a complete newbie to PHP, and am trying to teach myself how to make some scripts. So far I've done pretty good, but even after searching through all my books, articles, manuals,...
10
3160
by: Mike S | last post by:
Does anyone know the logic behind why in VB.NET the result of a floating-point division ('/') is -rounded- on being converted to an integer type, such as with statements like Dim x As Integer =...
6
10793
by: Christopher Key | last post by:
Hello, Could anyone tell me what the various C standards say on the subject of rounding during signed integer division. On my system, it always rounds towards 0: printf("%d", 3/2); // 1...
206
13093
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
4957
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7274
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
7323
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...
1
6984
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
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.