HTML / Javascript Rounding Numbers

Hi everyone,
I wonder if you can help me please.
Trying to set the output of a textbox to 2 decimal places and failing completely!
Please find code below:

	<script language="javascript">
	
	function multiply()
	{
	var ans=(document.form.one.value) * (document.form.two.value)
	document.form.answer.value=ans;
	}
	
	</script>
		
	<title>Home Page</title>
</head>
<body>

<!--The block of code below is HTML - this code sets out the webpage, placing boxes, buttons etc on
the form for the user-->

<center>

      <h1 style="color: darkgreen;"><big>Welcome to the main webpage</style></big></h1>
      <br/>

      <h2>
      <p style="background-color: orange;">Input your Rate of Pay and select or input the Hours you Worked this week 
      into the Input Boxes provided, then click the "Submit Enquiry" button to obtain the calculated result</style></p>
      </h2>
<br/><br/>

	<form name="form">
	Select or Enter the Weekly Hours Worked (Min 1 Max 168) <b><input type="number" name="one" min="1" max="168" required></b>
	<br/><br/>
	Enter the Rate of Pay  (Example 9.30)  £ <input type="text" name="two" required>
	<br/><br/>
	
	Weekly wage earned  £ <input type="text" name="answer">
	<br/><br/><br/>

	<input type="button" value="Submit Enquiry" name="now" onclick="multiply()">
	<input type="reset" value="Reset all fields">
	<br/><br/>

When I click the button I need the result to be rounded to 2 dec places.
Thanks in advance

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Thank you for your reply ieahleen magical girl, it was unforntunately not very helpful as it failed to address the issue I have, never heard of backticks. Now if you don’t mind, if you have something constructive to write in reply to my initail post - please write it and be helpful otherwise why waste your time?

Hello,

Please bear in mind that @ieahleen is a member of the forum moderation team, and that like any member of our community she should be treated with respect. Formatting your posts correctly allows us read them so we are able to accurately provide the help you are asking for.
The backtick key (`) can be found next to the 1 key on a standard US-QWERTY keyboard.

Here is a link for the Math.round() function, which is built in to javascript and may be helpful for you here.

1 Like

I wasn’t able to help you, but I could make so that who comes after can.
How could someone help if they can’t read your code?

Please next time you post a code snippet format it using backticks so it can be read.

3 Likes