However, javascript is a friendly loosely-typed language and if you use math operations on strings it will assume that you actually mean numbers.... but I'd rather be on the safe side and explicitly tell javascript which data type I'm currenty using...
@steelersfan88Apr 01.2004 — #Using that method, try:[code=php]variable = parseInt(parseFloat("010.6"))[/code]or to avoid the parseInt alltogether, try:[code=php] <script type="text/javascript">
var num = "010.6" num = (parseFloat(parseFloat(num).toString().substr(0,(parseFloat(num).toString().indexOf('.') > -1) ? (parseFloat(num).toString().indexOf('.')) : (parseFloat(num).toString().length)))) alert(num)
</script>[/code]It takes the string, makes in a number, then makes it a string, finds any period, cuts off the decimal, then returns back as a number. The first method simply makes the string a floating point decimal before converting to an integer ?
@steelersfan88Apr 02.2004 — #Giving that number just displays it in scientific notation. You could do a check to see if its less than one 1, then do it, such as:[code=php]<script type="text/javascript">
var num = "-.000007" num = (num < 1 && num > -1) ? 0 : (parseFloat(parseFloat(num).toString().substr(0,(parseFloat(num).toString().indexOf('.') > -1) ? (parseFloat(num).toString().indexOf('.')) : (parseFloat(num).toString().length)))) alert(num)
</script>[/code].
Strings are treated as numbers only when not using the plus operator, since it is also the concatenation operator. Other languages intelligently use the ampersand as the concatenation character to avoid this problem. "5"-"2" = 3; tested below:[code=php]<script type="text/javascript">