/    Sign up×
Community /Pin to ProfileBookmark

Format Number Problem

function CountUnitValue()
{
var oTxt1 = document.getElementById(“OpenMarketValue”)
var oTxt2 = document.getElementById(“PropertySaiz”)
var oTxt3 = document.getElementById(“UnitValue”)

if(oTxt1.value != “” && oTxt2.value != “”)
{
oTxt3.value = oTxt1.value / oTxt2.value

var formatTxt3 = oTxt3.value
alert(formatTxt3.toFixed(2))
}
}

</script>

I want to round it up to 2 digit number.
Above is my code, why error happen when i “alert(formatTxt3.toFixed(2))” , but when i wrote alert(formatTxt3), no error.

formatNumber(formatTxt3, “0.00”) also not working . ??????

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterMay 12.2005 — [b][size=3]Solution:[/size][/b]

Replace:
var formatTxt3 = oTxt3.value
with:
var formatTxt3 = oTxt1.value / oTxt2.value




[b][size=3]Explaination[/size][/b]

Take a close look at the following two lines:
<i>
</i>var formatTxt3 = oTxt3.value
alert(formatTxt3.toFixed(2))


Your retrieving the value of an element -- which is always a string.

If the element contains the letter [b]e[/b] then your setting

var formatTxt3 = "e"

If the element contains the number [b]3.14159[/b] then your setting

var formatTxt3 = "3.14159"

Which isn't a number but rather a string.


The reason why oTxt1.value / oTxt2.value works is because your dividing two strings from each other.

Try executing the following:
<i>
</i>alert(3/4)
alert("3"/"4")
alert(3+4)
alert("3"+"4")

The plus operator is the only operator that works with strings.

Each other operator automaticly converts the string into a number first.

Or in the case that the string doesn't contain a number it returns [b]NaN[/b]

(not a number)
Copy linkTweet thisAlerts:
@thiensyhauthorMay 12.2005 — wow! thanks a lot man!
Copy linkTweet thisAlerts:
@UltimaterMay 12.2005 — happy to help! my guestbook is always open for signing ? (refer to sig.)
Copy linkTweet thisAlerts:
@VladdyMay 12.2005 — For old C-Wolfs ??? and/or those in need of some heavy data formating. Enjoy.

http://www.vladdy.net/demos/printf.html

Those who were not even a project in the days of C - google printf manual first
×

Success!

Help @thiensyh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...