Hi all,

i need help with this code.

<html>
<head>
<SCRIPT LANGUAGE="VBScript"> 
<!--
Sub button_OnClick
  Dim TheForm
  Dim y
  Set TheForm = Document.form1
  If IsNumeric(TheForm.inputbox.Value) Then
    If TheForm.inputbox.Value >= 1 Then
      y = TheForm.inputbox.Value * 1.3
	  Alert("Your $AUD is $" & y)
    End If
  Else
    Alert("Please enter a numeric value.")
  End If
End Sub
-->
</SCRIPT>

</head>
<body>
<form name="form1">
	<input type="text" name="inputbox">
	<input type="button" value="convert" name="button">
</form>
</body>
</html>

How would i get it so when i enter a number and it gets converted, it comes back in $ format? eg if i enter 1, it comes back $1.3 but how would i get it to be $1.30 without just adding 0 on the end?

Thanks Wilko

Dani AI

Generated

As shows, the script is correctly computing y but the output is just the raw number string. As suggested, use VBScript's format functions to force two decimal places instead of tacking on characters.

VBScript options:

' convert input to a number, compute, then force two decimals
y = CDbl(TheForm.inputbox.Value) * 1.3
Alert("Your AUD is $" & FormatNumber(y, 2))

Use FormatNumber when you only need a fixed number of decimals. FormatCurrency(y, 2) will add the system currency symbol and locale formatting (group separators, negative parentheses), so it may not show "A$" explicitly.

Practical notes and gotchas:

  • If you want a guaranteed Australian-dollar label use a manual prefix: Alert("Your AUD is A$" & FormatNumber(y, 2)).
  • Always convert the input to a numeric type (e.g., CDbl or Val) after IsNumeric to avoid string concatenation.
  • Formatting routines will round the value (e.g., 1.345 -> 1.35 with two decimals).

Browser compatibility:

  • Client-side VBScript runs only in Internet Explorer. For cross-browser pages use JavaScript:
    var y = parseFloat(document.querySelector("input[name=inputbox]").value) * 1.3;
    alert("Your AUD is $" + y.toFixed(2));

    or for proper locale currency formatting:

    alert(new Intl.NumberFormat("en-AU", { style: "currency", currency: "AUD" }).format(y));

These approaches give consistent two-decimal output and safer, locale-aware formatting depending on the page requirements.

Look up the FORMAT function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.