Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1944
  • Last Modified:

Round to 1 Decimal Place

Hi

I need to write a function to round to 1 decimal place

Can someone tell me if what i have written is correct

Thanks

Anthony
<HTML>
<HEAD>
<TITLE>Round1dp
</TITLE>
<SCRIPT>
 
 
// Function to round to 1 decimal place
function round1dp(anyNumber)
// Function returns its argument to one decimal places of accuracy  
{
	return Math.round(10 * anyNumber) / 10;
}
 
 
 
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

Open in new window

0
antmoore123
Asked:
antmoore123
2 Solutions
 
Mike_CarrollCommented:
Yep, thats the way to do it. Ages old, tried and tested!
0
 
hankknightCommented:
You can test it with alert();
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Round</title>
<script type="text/javascript">
function round1dp(anyNumber)
// Function returns its argument to one decimal places of accuracy  
{
	return Math.round(10 * anyNumber) / 10;
}
alert(round1dp('34.54454'));
alert(round1dp('234.1232354454'));
alert(round1dp('7.762343454'));
</script>
</head>
<body>
</body>
</html>

Open in new window

0
 
antmoore123Author Commented:
Thanks
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now