JavaScript - Number To 2dp Without Rounding
Hello,
I need to be able to get a number to 2dp without rounding e.g. 14.756 needs to be 14.75 I tried .toFixed(2) but this rounds. I thought of converting to a string and performing string functions but it seems like over kill if there is an easier way. thanks Similar TutorialsTo Forum Thank you for being here. I have taken 2 calculators and edited them to get almost what i want. I need the final answer to round up to a whole number. The page is http://www.La-Dore.com/Goldleaf-Calculator.html The code i think you want to see is .... Code: <!-- end of script 1 begining of 2--> <SCRIPT LANGUAGE="JavaScript"> function perRound(num, precision) { var precision = 3; precision = parseInt(precision); var result1 = num * Math.pow(10, precision); var result2 = Math.round(result1); var result3 = result2 / Math.pow(10, precision); return zerosPad(result3, precision); } function zerosPad(rndVal, decPlaces) { var valStrg = rndVal.toString(); var decLoc = valStrg.indexOf("."); if (decLoc == -1) { decPartLen = 0; valStrg += decPlaces > 0 ? "." : ""; } else { decPartLen = valStrg.length - decLoc - 1; } var totalPad = decPlaces - decPartLen; if (totalPad > 0) { for (var cntrVal = 1; cntrVal <= totalPad; cntrVal++) valStrg += "0"; } return valStrg; } function clear_field(field) { if (field.value==field.defaultValue) { field.value='' } } //Division function doMath(form) { var firstnumber = 0; var secondnumber = 0; var tempvalue = 0; firstnumber = eval(form.firstnumber.value); //first number secondnumber = eval(form.secondnumber.value); //second number tempvalue = (firstnumber) / (secondnumber); form.calcresult.value = (tempvalue); //out } // --> </script> So I use Math.round to round my solution off to the nearest thousandth, but if I put in 6 for the number I am solving for it returns 1.333 When I multiply that by 3 it gives me 3.999 when I really want 4.0 Is there any way I can use fractions or something to get around this? My project: http://wdroom.com/factor.html Code: num = 1.025; alert(num.toFixed(2)); // - comes out as 1.02 num = 0.025; alert(num.toFixed(2)); // comes out as 0.03; Why is this? Code: function convertee(){ value = document.inputFormee.argument.value; if (isNaN(value)){ alert(value+' is not a valid entry'); return; } arg = (value/1.75975326); arg = (parseInt(arg*100)/100).toFixed(3); document.inputFormee.result.value = arg; } Any help much appreciated. Saz. I recently started trying to learn javascript and I decided to try an exercise I found somewhere. Basically it involves counting eggs into total, dozen, and gross. The problem is when I put in a number lower than a full dozen or gross it rounds up and says I have the full dozen intead of the fraction. Ex. I say I have 143 eggs, it says I have 12 dozen. Obviously I am 1 short. Apparently the method(I think that is what it is called?) rounds to the nearest number. Which methods? round either up or down? Here is the code... Code: <html> <head> <title></title> <script type="text/javascript"> function eggs(number) { var total=prompt("eggs",""); alert("You have "+total+" eggs."); var dozen=(total/12); alert("You have "+dozen+" dozen eggs."); var gross=(total/144); alert("You have "+gross+" gross eggs."); var fulldozen=(Math.round(dozen)); alert("You have "+fulldozen+" dozen eggs."); var fullgross=(Math.round(gross)); alert("You have "+fullgross+" gross eggs."); } </script> </head> <body> <form> <a href="javascript:eggs('number')"><input type="button" value="Eggs" name="Eggs" size="30"></a> </form> </body> </html> Thanks for reading. Edit: Also, it goes through the whole thing twice. It will prompt the first time, go through, then prompt again. It terminates after the second though. Hello guys I am doing a simple calculation. Here is the code Code: dose=1 A = document.rhogam.bleed.value B = document.rhogam.volume.value dose= (A*B)/3 dose=Math.round(dose*10)/10; this works fine and gives me an answer rounded to the tenths spot. the next step i need to do is round up by one if the tenths spot is less than 4 or round up by 2 if the tenths is 5 plus example 3.3=4 3.6=5 The code issue I am having is how do I read the tenths place in my result so that I can act on it? Thanks Jeremy I am currently using a price calculator - although it's really not a calculator; I have to enter every price manually. Currently, it works in half inch increments. I need it to do two things that it doesn't do currently: 1) Allow the customer to enter a dimension in 1/4 inch increments (1/8 would be even better, but I'm not requesting a miracle) 2) Make the result automatically round up to the nearest 1/2 inch Page containing calculator External file: sizecalc.js Hi there, To summarize my problem: For example: Sales : $ 5 (id=sales) Discount: 11% (id=disc) Total Discount: $ 0.55 (id=disc) Rounded Discount: $ 0.6 (1 decimal) (id=rounded) Net Sales: $ 4.40 (id=net_sales) document.getElementById("net_sales").value = sales - rounded.toFixed(1); Net Sales: $ <input id = "net_sales" type="Text" name="net_sales" size="4" value="<?php if (!empty($net_sales)) echo $net_sales; ?>"> I tried the above, it is working in the browser. However, it cannot be written into MYSQL (with PHP). What is wrong with my Javascript? Thanks. I have tried many ways to do this and have also searched the forum, no go. I was doing it manually doing this: Code: Math.round((#)*100)/100 But no go, so I found this function here on the forum: Code: function roundNumber(num, dec) { var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); return result; } No go, it works yes, don't get me wrong. But I am working on a invoicing system and it needs to be exact. For instance with this number: Quote: 0.27470000001 It returns 0.27, when in reality it should return 0.28 because of the 7 changes the 4 to 5 and then 5 changes the 7 to 8. But it doesn't work. While reading some things I found while searching google I see that Math.round doesn't work 100% the way it should mathematically. So what options do I have left, I can't believe that a simple rounding calculationg that excel can do ha ha JS cannot. Hi.. I have javascript code with computation, but I have problem in my result because it was not round off or fixed into two decimal places only. here is my code: Code: <script type="text/javascript"> function test_(cmpd) { var len_ = (document.frmMain.elements.length) - 1; for (i=0; i <= len_; i++ ) { var strPos_ = document.frmMain.elements[i].id.indexOf(cmpd) if (strPos_ != -1) { var strPos = document.frmMain.elements[i].id.indexOf("_"); var strId = document.frmMain.elements[i].id.slice(strPos + 1) + "_" + document.frmMain.elements[i].id.slice(0,strPos) // this is the computation that I need to fixed the result into two decimal places document.frmMain.elements[i].value = document.getElementById(strId).value * document.getElementById('mult').value; } } } </script> Thank you so much Wizards - I have a challenge! I am writing a timecard app. The stipulatioin is all Hours fields can only be whole hours or half hour increments. For example, 1.0 is good, 1.5 is good, 1.2 is bad, 1.9 is bad. I would like to have the field automatically change a value of 1.9 to a value of 2.0 (perhaps onKeyUp). I would like to have the field automatically change a value of 3.2 to a value of 3.0. In a nutshell - only allow whole and half hour values. Otherwise round up OR round down. Thanks for any help you can provide. I am successfully using the Pengoworks plugin for calculations - absolutely priceless. If you think modifications can be made to it, plz let me know. I have a function below where every time a question is submitted, it will add a new row in the table with a textbox which allows numbers entry only. My question is that I don't know how to code these features in this function: 1: I want the text box to be between 0 and 100, so if text box contains a number which is above 100, it will automatically change the number to the maximum number which is 100. Does any one know how to code this in my function below in javascript: Code: function insertQuestion(form) { var row = document.createElement("tr"); var cell, input; cell = document.createElement("td"); cell.className = "weight"; input = document.createElement("input"); input.name = "weight_" + qnum; input.onkeypress = "return isNumberKey(event)"; cell.appendChild(input); row.appendChild(cell); } I am trying to figure out how to make a random number I can plug into a script count down from that number at certain times of the day until it reaches 0. I would like it to reset itself at midnight every day. I'm trying to make it work with a script I found on here that resets itself at midnight every day. So instead of it counting down too fast, it would count down to the next number after a randomly generated number of minutes until it reaches 0. But it wouldn't necessarily have to end at 0 at midnight. It could go from 845 to 323 at the end of the day at a slower pace. Is that possible?
When I used toFixed() method on a number, I thought that this method round a number to a specified approximation, but I got a surprising result, the number became string! 15.23689 .toFixed ( 2 ) ==> "15.24" So does it convert the number into string? I am delving into the coding world and while I understand the basic principle of cookies, conditional statements, arrays, etc... I am still learning how to properly implement them. Any asistance with the following situation would be greatly appreciated and help with my learning as I try to reverse engineer the logic. I have looked around the web and this forum with little success. If the situation below is too complicated, I would really appreciate even a shove in the right direction regarding the logic. ----------------- How could I show a preset counter which counts up from a preset, beginning number toward a preset, end number? I imagine the increment and speed is set be the difference between the two numbers and a timeframe. Assumptions: I would rather not set the increment but edit the end number to show a steady increase. As I update that number, the increment adapts dynamically. I would want the number/script to be useful, so it should not refresh to the beginning number on each page load (i.e. num=0). When a visitor comes to the page, it must seem like the counter has been steadily been increasing in their absence. Coke or Pesi did something similar one time regarding cans sold to date (doubt it was plugged into a DB somewhere but rather based on a steady sales figure) and it was pretty cool. All the best! Ooook so this is probably the stupidest question ever. I know in java you can take advantage of a number being an int (not double) and divide by something without getting a remainder. But in javascript you dont declare what kind of variable something is. So my problem is: Given any double or triple digit number, how do i get all but the last digit. Like if i have 13, I need to make an int with 1. If it's 103 i need one with 10. Thanks!! I have a dynamic form that uses Code: var input_box = $("<input type=\"text\" id=\"ac\" >") I need to have a unique id for each input that is displayed. How can I do this? I can't use vars or anything in there as variables don't work in this as it is? Hi. I'm trying to make a web page using javascript for validation, but after the last 2 functions I wrote, 2 functions don't work anymore. I attached the file so you can see for yourself. And I know there are a few things I could do to the html, but I'm saving those for last. Any idea why the scripts don't work is much appreciated. (the functions that don't work a "cont_pag1" and "cont_pag2"...btw...they worked just fine until I wrote the last few functions (the ones for the pag4 div))...And sorry for the really strange language...
Hi All, Im a total novice with coding other than html/css. I have a video xml which, in which i need a new feature, could you help me with that? Basically my video is being entered with a time of it - for example 30sec. I have start and end trackers firing for the video but now i want to add impression trackers at 25%, 50%, 75%. I managed to do it using specifying seconds for the calls, but it becomes a pain to recalculate seconds for every video. Any ideas I could follow? Thanks, AD Hi, I have a string stored in a variable var = "4.25 houses". I would like to only extract the currency value, ie "4.25" from this. Is this possible? Also I would like to divide and multiply the new variable I get... does this number need to be converted or something? Thanks in advance! |