# Decimals handling in PHP

PHP Round Off Decimal or Convert to Decimal Point

Round Off Decimal

Round a number up or down or Round off a floating decimal point number using PHP’s functions round().

Example. round();

<?php
echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2); // 5.05
echo round(5.055, 2); // 5.06
?>

Convert to Decimal Point

Example. number_format();

<?php
\$number = “28”;
\$number = number_format(\$number, 2);
echo \$number; // equals “28.00”
?>

PHP number formatting for decimal places

PHP has powerful math functions.
Formatting a number is required while displaying or preparing for some other processing.
One common requirement is displaying values after a division with fractions.
One way is to round off the number or we can format the number with specifying whether to display the digits with decimal or without decimal or number of digits to display after the decimal.
We can place comma ( ,) after a group of thousands. We can use other symbols in place of comma (,) for thousands and we also can replace (.) dot with symbol of our choice to be used as decimal.
Using PHP number_format function we can do all these formatting. Here are some examples for number formatting.

\$number=2512589.66785;
echo “Number = \$number “;
echo ” The value after formating = “.number_format(\$number);

// The above line will display 2,512,590 ( without decimal and rounded)

echo “<br> The value after formating = “.number_format(\$number,3);
// Will display 2,512,589.668 ( with , after each thousand and 3 digits after decimal
// rounded )

echo “<br> The value after formating = “.number_format(\$number,3,”#”,”:”);
// Will display 2:512:589#668 ( with : after each thousand and # as decimal symbol )

Happy Coding 🙂

# Multiple HTML Input Elements with same name in JavaScript, ASP and PHP

Hi,

Some times we came around a tricky situation where we have to use multiple html input elements with same name in our pages and need to tackle them with JavaScript (in client side) or ASP and PHP (server side).

Suppose we have multiple text area elements in a HTML page. The name of these text areas are “txtArea”. And the problem is we need to validate the text lengths in these text boxes as we donot wish the user to enter more than 250 characters.

File: MultipleTextArea.html

<html>
<title>Multiple Text area Validation</title>
<body>
<form name=”form1″ method=”post”>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<input type=”submit” name=”Submit” value=”Submit Form”/>
</form>
</body>
</html>

Now here we can see that we have 7 Text Area elements with the same name as “txtArea”.  And we want to validate these text areas so that users can not insert more than 255 charaters.

Now to validate this with JavaScript we have to made small changes in the code to add a Validation function and call it on the “onsubmit” method of the Form.

File: MultipleTextArea.html

<html>
<title>Multiple Text area Validation</title>
<script type=”text/javascript”>
function check(){
var form = document.forms;
var txtS =  form[“txtArea”];
var len = txtS.length;

for(i=0;i<len;i++)
{
if(txtS[i].value.length > 255)
{
txtS[i].focus();
return false;
}
return true;
}
</script>
<body>
<form name=”form1″ method=”post” onsubmit=”return check()”>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<input type=”submit” name=”Submit” value=”Submit Form”/>
</form>
</body>
</html>

Now if the Javascript function “check()” we point the text area lot with form[“txtArea”] and form is the only form in this html. We can point to the form as document.forms or document.form .

So now we have all the text areas in the array form[“txtArea”] . We can view the length of the Text Area array as form[“txtArea”].length

To get the text areas one by one we can use a for loop and use txtS[i].value to get the value of the i th text area and txtS[i].value.length to get the length of the value of the i th Text Area.

We can validate the value by using a simple if – else statement.

The Error which might come in this situation : If we wish to populate the text areas programmatically then we may not now how many text areas are in the form. There might a single text area.

If the form contains a single text area then our validation javascript function will not work as needed. As it will generate a Javascript error in this line :  var len = txtS.length
As this form holds a single text area the len will be null or we can say undefined.

So if the len is null we can say there is only one text area present, and we can access its value by form[“txtArea”].value and length by form[“txtArea”].value.length

So we have to modify the javascript function as

function check(){
var form = document.forms;
var txtS =  form[“txtArea”];
var len = txtS.length;

if(null == len){
if(form[“txtArea”].value.length > 255)
{
form[“txtArea”].focus();
return false;
}
}else{
for(i=0;i<len;i++)
{
if(txtS[i].value.length > 255)
{
txtS[i].focus();
return false;
}
}
}
return true;
}

Now our Javascript is finalized for our need. Now we just need to get the values properly in our scripting language like ASP and PHP.

ASP Interpretaion of Multiple Input elements with Same Name:

In ASP we have to write a simple code to accomodate our need.

First let us change the file to a ASP file. So the file name will be like MultipleTextArea.asp

The file:

<html>
<title>Multiple Text area Validation</title>
<script type=”text/javascript”>
function check(){
var form = document.forms;
var txtS =  form[“txtArea”];
var len = txtS.length;

if(null == len){
if(form[“txtArea”].value.length > 255)
{
form[“txtArea”].focus();
return false;
}
}else{
for(i=0;i<len;i++)
{
if(txtS[i].value.length > 255)
{
txtS[i].focus();
return false;
}
}
}
return true;
}
</script>
<body>
<form name=”form1″ method=”post” onsubmit=”return check()”>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<input type=”submit” name=”Submit” value=”Submit Form”/>
</form>
</body>
</html>

Now after the user has validated all the aext areas and submits the form it will post back to itself.
So at the top of this ASP file we have to write our ASP code to get this form data.

All the text areas will come together delimiting with a comma. So all the values for the input element txtArea will be combined into one single string delimitted by commas (,).

So we can split the value with comma ane iterate through all the values one by one.

The ASP will be like:

<%

myTXT = Request.Form(“txt”) & “,”
‘ADD a comma at the end to split perfectly and get the last value easily
allTXT = split(myTXT,”,”)
‘Now split the values with comma

For i = 0 to ubound(allTXT)-1
‘Set the loop exit criteria to 1 less than the length of the string, as we have added a extra comma

Response.Write(“[” & i & “] = ” & allTXT(i))
‘Print the values one by one
‘Here you canactually do your coding

Next
‘Loop in the for loop

%>

So finally your ASP file will look like:

<%
myTXT = Request.Form(“txt”) & “,”
‘ADD a comma at the end to split perfectly and get the last value easily
allTXT = split(myTXT,”,”)
‘Now split the values with comma

For i = 0 to ubound(allTXT)-1
‘Set the loop exit criteria to 1 less than the length of the string, as we have added a extra comma

Response.Write(“[” & i & “] = ” & allTXT(i))
‘Print the values one by one
‘Here you canactually do your coding

Next
‘Loop in the for loop

%>
<html>
<title>Multiple Text area Validation</title>
<script type=”text/javascript”>
function check(){
var form = document.forms;
var txtS =  form[“txtArea”];
var len = txtS.length;

if(null == len){
if(form[“txtArea”].value.length > 255)
{
form[“txtArea”].focus();
return false;
}
}else{
for(i=0;i<len;i++)
{
if(txtS[i].value.length > 255)
{
txtS[i].focus();
return false;
}
}
}
return true;
}
</script>
<body>
<form name=”form1″ method=”post” onsubmit=”return check()”>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<textarea name=”txtArea”></textarea>
<input type=”submit” name=”Submit” value=”Submit Form”/>
</form>
</body>
</html>

Thats all in ASP. I think it will help some body in this big universe 😉

Now We will look at PHP interpretation in the next part.