• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1952
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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