PHP - Ltrim & Rounding - How To Combine Syntax?
$f15 = 0.345677 (output from sql data pull)
<?php echo number_format ($f15,3); ?> output = 0.345 <?php echo ltrim($f15,"0"); number_format ($f15,3); ?> output = .345677 How do I combine these so that I am BOTH rounding the result AND trimming a leading zero? Seems I can get one or the other, but not both. Suggestions on the syntax? Similar TutorialsHello all, I want to be able to trim data using an array. For example: I have: REF*19**PHILADELPHIA 02 REF*GG*CUT PILE Which both are separate lines in an array. I want to remove REF*19** and REF*GG* from each and be left with PHILADELPHIA 02 and CUT PILE. I was hoping I could use an array to remove the unwanted data from each line and rebuild the array? Here's what I have currently. Which of course, throws an error. I tried in_array() but maybe I just didn't use it correctly? Thanks for any help you can provide me. Code: [Select] <?php $unwantedData = array ('REF*19**', 'REF*GG*' ); $lineArray = array('REF*19**PHILADELPHIA 02', 'REF*GG*CUT PILE'); $newEdiArray = ""; for ($i=0; $i<count($lineArray); $i++) { $line = $lineArray[$i]; $newLine = ltrim( $line, $unwantedData ); $newEdiArray[] = $newLine; } ?> Hi all, Can anyone please help me with my problem. I want to know how to round a decimal - eg. 12.34 to the nearest .10 so it would be 12.30 or if 12.35 go to 12.30 or 12.40. // my example: round(12.34, 2); // I want to to go to: 12.30 Any help is greatly appreciated. So I just started working with php (not programming) a couple days ago. In this formula, I need to round something down to the nearest 5 OR 0. Whatever comes first. Any thoughts on how i might do this? Here's what I have so far. <?php $magicLevel = $_POST['magicLevel']; $playerLevel = $_POST['playerLevel']; $rune = $_POST['rune']; //gets the value "lmm" $maxDamage = 0; switch($rune) { case "lmm": $maxDamage = ($playerLevel*0.2)+($magicLevel*0.81)+4; break; } echo $maxDamage; ?> What I need to round down is this part: ($playerLevel*0.2) Thanks in advance! I want to round off an integer vaule to 10-6 grd from 52.71666666 to 52716666 0.926888888 to 926888 does anyone know a simple way to do this? How can I round this with 2 decimals: Code: [Select] echo " $row[team_treasure] "; I've tried round($row[team_treasure],2) but doesn't seem to work inside my echo tags :/ hi there all, I need some help for rounding currency issue.. example: 0.98 round to 0.95 0.95 when round = 0.95 0.91 roun to 0.95. mean. i wanna round the end of number 0.98. if the end of number is 0.01,0.02,0.03&0.04 it would be automatic round to nearest 0.05 *0.01 round = 0.05 *if the mineral water =$0.71 i should get $0.75 and if the end of number is 0.06,0.07,0.08,0.09 it would automatic round to nearest -0.05 *0.06 round = 0.05 *if the mineral water = $0.79 then i should get $0.75 i had googling this for 2months and no answer.. please help me.TQ I am trying to take a number (three for example) and want to round it to five, or sixty-seven up to seventy. I looked at round and ceil, but those don't seem like they are the right things. Thanks, Joseph I am very new to PHP and programming in general. About 3 weeks into this. I have this simple expression and some echo in my code. $x1 = $lat_b + $lat_a1; echo "<br>lat b: ".$lat_b; echo "<br>lat a1: ".$lat_a1; echo "<br>x1: ".$x1; $lat_b and $lat_a1 are derived from some xml extractions from a yahoo geocode api (that part works fine). The results of the expression and echos above are as follows: lat b: 40.402866 lat a1: 40.252590 x1: 80 Why is $x1 rounding automatically on me? Later, I substituted the expression above with $x1 = 40.402866 + 40.252590 ; The new result (shown below) makes sense to me. lat b: 40.402866 lat a1: 40.252590 x1: 80.655456 Hey Everybody, I was replying to someone elses question about number formatting and came up with a class that will format a number of any length with decimal or not, without rounding. It works perfectly, but after a comment by a noteworthy member my curiosity was aroused. Is there an easier/faster/better way to achieve this?? My Object: <?php /*GHETTO NUMBER FORMATTER v1.0*/ class ghettoNumber { private $input; private $output; public $whole; public $decimal; public $number; function __construct($input){ $this->input = $input; $this->output = $output; $this->number = $number; $this->whole = $whole; $this->decimal = $decimal; } private function ghettoFormat(){ //Split number at the .(decimal) $this->number = explode('.', $this->input); //Define the whole number $this->whole = $this->number[0]; //Format the whole number $this->whole = number_format($this->whole, 0,'', ','); //Define the decimal $this->decimal = $this->number[1]; //Format the decimal $this->decimal = rtrim($this->decimal, '0'); if(is_string($this->input)){ $this->output = $this->decimal != '' ? $this->whole.'.'.$this->decimal : $this->whole; } else{ $this->output = '<strong>ERROR:</strong> Input arg must be passed as type: <em>string</em>'; } //Return result return 'Original - '.$this->input.'<br> GhettoFormat - '.$this->output.' <br><br>'; } public function makeItGhetto(){ return $this->ghettoFormat(); } } ?> Sample Instantiation: $ghettoFormat = new ghettoNumber('12565456565.123401201001210000'); print $ghettoFormat->makeItGhetto(); I will reiterate my question once more... Is there, in your opinion, a BETTER way to achieve this (number formatting w/o rounding)? Thanks People, E I need to round a number based on it's decimal. 3 or below should round down, 4 should round up to 5, 6 and 7 should round down to 5; 8 and 9 should round up to the next whole number. The below is returning 3 from 2.16. Code: [Select] <?php $a=2.16; $i=round($a, 1); $b=$i; $i=explode(".", $a); $decimal=$i[1]; if($decimal < 5){ $j=floor($b); $i= $decimal > 3 ? $j + .5 : $j; echo $i; } else{ $j=ceil($b); $i= $decimal < 8 ? $j - .5 : $j; echo $i; } ?> EDIT: The explode should have been on $i not $a. Bascially i add a couple numbers up and display the sum. sometimes the number is something like this: 234.237 i want it to display : 234.24 i realize its needs "round()" in it but i dont know how to use it.. any help? Here is the number: $GrandTotal = "68.835"; $Deposit = round($GrandTotal , 2) * .5; echo $Deposit; I am looking for: 68.84 but the function doesn't seem to work right. Any help? Hi, Having trouble rounding the output. I would need to be sure the output is not longer than 2 spaces after comma. Any ideas? My code Code: [Select] <script type="text/javascript"> $(document).ready(function() { $("<?php echo "#".$input_end_time; ?>").blur(function(){ var start_time = $("<?php echo "#".$input_start_time; ?>").val(); var end_time = $("<?php echo "#".$input_end_time; ?>").val(); $.getJSON("include/timedifferencecalculation.php?s="+start_time+"&e="+end_time, function(data){ var success = data.success; if (success == 1) { $("<?php echo "#".$input_difference; ?>" ).val(data.hours + " h " + data.minutes + " m "); if (data.hours + data.minutes/60 >= 6){ $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60 - 0.5); }else { $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60); } }else { $("#message").text("Viga! " + data.reason); } }) }); }); </script> Code: [Select] This is before $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60 - 0.5); This is what im trying to get but its not working. $("<?php echo "#".$hours_difference; ?>").val( round(data.hours + data.minutes/60 - 0.5), 2 ); Hi fellas, Can anyone show me why my script is still rounding up / down? printf ("<tr> %s Avg gun Ahit %%</td> %s %.2f %%</td></tr>\n",$tdo,$tdor,number_format($ahit/$fired*100, 3, '.', '')); Do I have a syntax error as I cannot get my math equation to display more than 2 decimal places even though it's set to 3? Any help or advice would be really great. I have been looking at this code most of the morning and do not have a clue what is wrong with the code. I am hoping its not a stupid mistake, can someone please help me out? thank you
<title>Inputing Travel Detials</title> <header> <h1 align="center"> Adding Travel Detials </h1> <body> <p> <center><img src="cyberwarfareimage1.png" alt="Squadron logo" style="width:200px;height:200px" style="middle"></center> <table border="1"> <tr> <td><a href="index.php"> Home Page </a></td> <td><a href="administratorhomepage.html">Administrator Home Page </a></td> <td><a href="viewhomepage.html">View Home Page </a></td> <td><a href="Inputhomepage.html">Input Home Page </a></td> <td><a href="traveldetials.html">Enter More Travel Detials </a></td> </table> </p> <?php include "connection.php"; $Applicant_ID = $_POST["Applicant_ID"]; $Method_Of_Travel = $_POST["Method_Of_Travel"]; $Cost = $_POST["Cost"]; $ETA = $_POST["ETA"]; $Main_Gate_Advised = $_POST["Main_Gate_Advised"]; $query = ("UPDATE `int_board_applicant` SET `Method_Of_Travel`=`$Method_Of_Travel', `Cost`=`$Cost', `ETA`='$ETA', `Main_Gate_Advised`='$Main_Gate_Advised' WHERE `Applicant_ID`='$Applicant_ID'"); $result = mysqli_query($dbhandle, $query) or die(mysqli_error($dbhandle)); if($result){ echo "Success!"; } else{ echo "Error."; } // successfully insert data into database, displays message "Successful". if($query){ echo "Successful"; } else { echo "Data not Submitted"; } //closing the connection mysqli_close($dbhandle) ?> Ok this is puzzleing. I am geting "Could not delete data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1". but its is deleting the entry that needs to be removed. The "1" is the entry. Just not sure what is causing the error. I do have another delete php but I have put that on the back burning for the time being.
<?php $con = mysqli_connect("localhost","user","password","part_inventory"); // Check connection if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } else { $result = mysqli_query($con, "SELECT * FROM amp20 "); $amp20ptid = $_POST['amp20ptid']; // escape variables for security $amp20ptid = mysqli_real_escape_string($con, $_POST['amp20ptid']); mysqli_query($con, "DELETE FROM amp20 WHERE amp20ptid = '$amp20ptid'"); if (!mysqli_query($con, $amp20ptid)); { die('Could not delete data: ' . mysqli_error($con)); } echo "Part has been deleted to the database!!!\n"; mysqli_close($con); } ?> Hi guys
I have this code below and all works fine when submitting this online application apart from when someone types either ' # & into one of the comment fields in which it throws up the error. Have tried various fixes from across the internet but no joy. Can anyone offer suggestions?
<?php
$con = mysql_connect("localhost:3306","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('sfapp', $con);
$sql="INSERT INTO 'sfapp' ('surname_add','forename_add','dob_add','hometele_add','mobiletele_add','homeadd_add','siblings_add','schoolname_add','headname_add','schooladd_add','schooltele_add','schoolem_add','alevel_add','personstate_add','nameprovided_add','pe_add','se_add','PredGrade_Art','PredGrade_AScience','PredGrade_BusStudies','PredGrade_Electronics','PredGrade_EnglishLang','PredGrade_EnglishLit','PredGrade_French','PredGrade_German','PredGrade_Geog','PredGrade_Graphics','PredGrade_History','PredGrade_Maths','PredGrade_SepScience','PredGrade_ProductDesign','PredGrade_Spanish','PredGrade_Other','Gender_Male','Gender_Female','Sub_EnglishLit','Sub_Maths','Sub_FurtherMaths','Sub_Biology','Sub_Chemistry','Sub_Physics','Sub_French','Sub_German','Sub_Spanish','Sub_Geography','Sub_History','Sub_RE','Sub_FineArt','Sub_Business','Sub_Computing','Sub_GlobPersp','Sub_DramaAndTheatre','Sub_PE','Sub_Dance','Sub_Politics','Sub_Psychology','Sub_Sociology','readprospect_chk','Sib_Yes','Sib_No','Current_Student_Yes','Current_Student_No','I_Understand_chk','Current_Education_chk','Local_Care_chk','Staff_Cwhls_chk','Sub_Film')
VALUES
('$_POST[surname_add]','$_POST[forename_add]','$_POST[dob_add]','$_POST[hometele_add]','$_POST[mobiletele_add]','$_POST[homeadd_add]','$_POST[siblings_add]','$_POST[schoolname_add]','$_POST[headname_add]','$_POST[schooladd_add]','$_POST[schooltele_add]','$_POST[schoolem_add]','$_POST[alevel_add]','$_POST[personstate_add]','$_POST[nameprovided_add]','$_POST[pe_add]','$_POST[se_add]','$_POST[PredGrade_Art]','$_POST[PredGrade_AScience]','$_POST[PredGrade_BusStudies]','$_POST[PredGrade_Electronics]','$_POST[PredGrade_EnglishLang]','$_POST[PredGrade_EnglishLit]','$_POST[PredGrade_French]','$_POST[PredGrade_German]','$_POST[PredGrade_Geog]','$_POST[PredGrade_Graphics]','$_POST[PredGrade_History]','$_POST[PredGrade_Maths]','$_POST[PredGrade_SepScience]','$_POST[PredGrade_ProductDesign]','$_POST[PredGrade_Spanish]','$_POST[PredGrade_Other]','$_POST[Gender_Male]','$_POST[Gender_Female]','$_POST[Sub_EnglishLit]','$_POST[Sub_Maths]','$_POST[Sub_FurtherMaths]','$_POST[Sub_Biology]','$_POST[Sub_Chemistry]','$_POST[Sub_Physics]','$_POST[Sub_French]','$_POST[Sub_German]','$_POST[Sub_Spanish]','$_POST[Sub_Geography]','$_POST[Sub_History]','$_POST[Sub_RE]','$_POST[Sub_FineArt]','$_POST[Sub_Business]','$_POST[Sub_Computing]','$_POST[Sub_GlobPersp]','$_POST[Sub_DramaAndTheatre]','$_POST[Sub_PE]','$_POST[Sub_Dance]','$_POST[Sub_Politics]','$_POST[Sub_Psychology]','$_POST[Sub_Sociology]','$_POST[readprospect_chk]','$_POST[Sib_Yes]','$_POST[Sib_No]','$_POST[Current_Student_Yes]','$_POST[Current_Student_No]','$_POST[I_Understand_chk]','$_POST[Current_Education_chk]','$_POST[Local_Care_chk]','$_POST[Staff_Cwhls_chk]','$_POST[Sub_Film]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
?>
<?php
//if "email" variable is filled out, send email
if (isset($_REQUEST['pe_add'])) {
//Email information
$admin_email = $_REQUEST['pe_add'];
$forename = $_REQUEST['forename_add'];
$email = "autoreply@testing.com";
$subject = "Application";
$desc =
"Dear $forename
Thank you for submitting your online application, we will be in touch shortly.
"
;
//send email
mail($admin_email, "$subject", "$desc", "From:" . $email);
//Email response
echo "Thank you for contacting us!";
}
//if "email" variable is not filled out, display the form
else {
?>
If you are seeing this, you need to go back and fill out the Personal Email section!
<?php
}
header("location:complete.php");
mysql_close($con)
?>
Thanks in advance.
Hello. This is my first post here. I am not an expert developer but I generally manage to do what is asked of me. However, now and then I'm stumped, like now... I have an online database system for our company in which staff can enter JOB related data, this includes 2 dimensions which they enter in millimetres (mm). Here's an example
$dimension1 might be entered as 850 Firstly, I need to round both dimensions up to the next 100mm so the 850 in this example would become 900 and the 1447 would become 1500. Once I've done that, I need to calculate the square metres (900mm x 1500mm / 1000 was my first thought) but that raised the second thing; adding a decimal point in the result. 900 x 1500 / 1000 = 1350 but I need the result to be 1.350 I know my way around basic PHP code but I'm no expert by any stretch. If anyone can help me understand how to get this calculation done I would be hugely appreciative. Thank you Martin HI. I have written a mysql database query to add up invoice totals. It seems to be automatilaly adding .01 to the result when I run a group by. Let me explain more ..
When I do a select on the field that I am adding up the result is $132.25 - the query is as follows
select ttl_invoice_due from 65_it where st_id = 96011;
When I run the following query that uses sum and group by - the $132.25 becomes $132.26 - the sum query is
SELECT 65_st.job_compid, 65_client.uid_client, 65_client.comp_name, Sum(65_it.ttl_invoice_due) AS SumOftotal_due, Sum(65_it.ttl_invoice) AS SumOftotal_invoice, Sum(65_it.ttl_invoice_payments) AS SumOftotal_amnt_paid FROM 65_client INNER JOIN (65_st INNER JOIN 65_it ON 65_st.st_id = 65_it.st_id) ON 65_client.uid_client = 65_st.uid_client WHERE 65_st.uid_client = 1905 and 65_st.job_compid = 1 and 65_st.sales_date<=curdate() GROUP BY 65_st.job_compid, 65_client.uid_client HAVING (((Sum(65_it.ttl_invoice_due))<>0)) order by 65_client.comp_name;
The 65_it.ttl_invoice_due field is defined in the table as Double(12,2)
I dont know why the sum is returing $132.26 where it should be $132.25 as in the simple query ?.
|