473,394 Members | 1,699 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,394 software developers and data experts.

Calculation script

28
I am looking to make a script that does a calculation for me.

The user is to enter two variables, height (H) and width (W), click "calculate" and the value will appear, hopefully on the same page.

The formula will be (((H*W)+150)*1.65) (pretty easy)

However, I would like it to round up to the nearest 10th. So if the value comes out to 411 I would like it to display 420. If the solution is 579 it will round up to 580.

Sounds easy but I am not sure where to start.

Any guidance will be appreciated.

Thanks!!!!
Mar 6 '08 #1
8 1651
mrking
28
This is my scripts so far:

[PHP]
<?PHP
$cost = ((($_REQUEST["height"]*$_REQUEST["width"])+150)*1.65);
echo "The cost of the commission will be " .$cost;
?>
[/PHP]

Expand|Select|Wrap|Line Numbers
  1. <form action="calculate.php" method="get">
  2. <td align="left">
  3. Height:<input type="text" size="4" name="height">
  4. </select>
  5. </td>
  6. <td align="left">
  7. Width:<input type="text" size="4" name="width">
  8. </select>
  9. </td>
  10.  
  11.  
  12.  
  13. <td colspan="2" align="center">
  14. <input type="submit" value="Calculate">
  15.  
This works now, but how do I get it to round up to the nearest 10 and how do I get it to display on the same page after I hit "Calculate"?

Thanks!!!

PS> FWIW, My site is built with WordPress
Mar 6 '08 #2
mrking
28
i've tried this:

[PHP]
<?

$cost=((($_REQUEST["height"]*$_REQUEST["width"])+150)*1.65);

echo "The cost of the commission will be " .round($cost);

?>
[/PHP]


That just rounds it up to a number with no decimals. Any advice on how to round it up to the nearest 10th?
Mar 6 '08 #3
Markus
6,050 Expert 4TB
[php]
<?php
function t_round($num) {
return ($num % 10 >= 5 ? ceil($num / 10)*10 : floor($num / 10)*10);
}
print t_round(4);
?>
[/php]
There is no specified function for doing this, but i found that ^ after some lurking.
Quite simple, actually..
If you don't know what's going on, check out the ceil() and floor() functions.
Mar 6 '08 #4
Markus
6,050 Expert 4TB
Then that defeats the point of this forum, doesn't it?
Mar 6 '08 #5
ronverdonk
4,258 Expert 4TB
Then that defeats the point of this forum, doesn't it?
Sorry I interrupted, but there was a spammer hanging around all forums. That is why I removed hist posts and he was banned for life.

moderator
Mar 6 '08 #6
Markus
6,050 Expert 4TB
Sorry I interrupted, but there was a spammer hanging around all forums. That is why I removed hist posts and he was banned for life.

moderator
Yeh, i noticed.
I was going to PM you but you werent online
Mar 6 '08 #7
dlite922
1,584 Expert 1GB
I am looking to make a script that does a calculation for me.

The user is to enter two variables, height (H) and width (W), click "calculate" and the value will appear, hopefully on the same page.

The formula will be (((H*W)+150)*1.65) (pretty easy)

However, I would like it to round up to the nearest 10th. So if the value comes out to 411 I would like it to display 420. If the solution is 579 it will round up to 580.

Sounds easy but I am not sure where to start.

Any guidance will be appreciated.

Thanks!!!!

It IS possible to do this the old fashion way.

Lets say you start off with 514 and you want to end up with 520

What you do is divide by 10:
51.4

do the round and add one
round(51.4) will give you 51
add one will give you 52

then multiply by ten!

that will give you a round up to 10.

if you want to round to 100, just divide and multiply by 100.

The other thing to consider is what if you get 500, do you want 510?

The way you can check for this is to divide by 100 and see if you get an integer number. 500/100 = 5 (hint: check for float() or presence of "." (decimal) in it.

514/100 will of course give you 5.14.


Hope that helps, good luck.
Mar 6 '08 #8
mrking
28
Perfect! Thanks everyone!!!
Mar 7 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: maceo | last post by:
I have some code that extracts the data from a table and performs a calculation (total time) on one of the columns. Here is the code: <?php /* Database connection */
5
by: Michael Hagstrom | last post by:
Good Day - I have written a form which collects input for a fare calculation. The form collects the following: 1) Departure or Destination Group (1,2,or 3) 2) Number of Adults 3) Number of...
1
by: John Mason | last post by:
Hi, I am trying to figure out how to do a client-side calculation based on textbox values, using asp.net, without performing a postback. I have 2 textboxes... <asp:textbox runat="server"...
8
by: rdavis7408 | last post by:
I am attempting what I would think would be a simple calculation of the cost of traveling a single mile. But I can not figure this out. The following is my script. Any help would be appreciated. ...
6
by: David | last post by:
Hi, I have the following calculation I am trying to work out ..... Not sure how ? Variable 1 = minstock Variable 2 = oDict(oKey) I need to test if my variable 'minstock' <= 40% of...
2
cassbiz
by: cassbiz | last post by:
I may be in the wrong forum so Ronald don't shoot :) In my code I have an option box to choose a number - works fine. I want to carry over the new value to another field to do a recalculation. ...
3
by: Gretsch | last post by:
Web, html, javascript, Hi, I need to calculate the time since this .htm file was last modified. {which I can then use in a calculation, rather than display, so days&decimals format would be OK}...
4
by: gregincolumbus | last post by:
I am trying to get the financial calculation on this to trigger whenever there is a change to select1. Right now, the user has to click on select2 to trigger the changes. Ideally, a change of...
11
by: mrking | last post by:
Hi, First time in the Java section here. I have an HTML form and a PHP script to do a simple calculation but it has to take me to a different page for the results. The calculator is really...
3
by: printline | last post by:
I am need of a simple price calculation script that will give users a price on a Printed Circuit Board. Here is an example: I have a panel that is 18 dm˛. A customers wants 1 PCB that is 100mm...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
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...

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.