JavaScript - Is There Any Way To Stop This From Rounding Please ?
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. Similar TutorialsSo 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 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 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 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. 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? 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 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 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. To 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> 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.. 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 Help! I'm not very good with html/css or any web stuff, my friend wants a blogger for his videos that I'm currently working on. Ive managed to create a splash page that features one video and an enter button. The only problem I am having is, one you click on 'wood + wires' it should go into the blog part of the page, which it does, but the sound of the video keeps playing. Is there any way to make it stop? This is the blog http://brennygee.blogspot.com and this is the code I'm using for the splash page... Please help! <!-- Welcome page Start by http://bloggersentral.blogspot.com/ --> <!-- HTML part --> <div style="padding-top:0px;"> <a id="EPEntryButton" onclick="document.getElementById("HTML88").style.display="none";document.getElem entById("Text88").style.display="none""><iframe src="http://player.vimeo.com/video/14285815?byline=0&portrait=0&color=ffffff" width="649" height="365" frameborder="0"></iframe></a> <a id="EPEntryButton" onclick="document.getElementById("HTML88").style.display="none";document.getElem entById("Text88").style.display="none""><img src="http://i640.photobucket.com/albums/uu121/brennygee/WWENTER3.gif" /></a> </div> <!-- CSS part --> <style> #welcome-wrapper{width:650px;margin:0 auto;height:0px;text-align:center;} /* welcome message widget */ #Text88, #EPEntryButton, #EPGrab {position:relative;z-index:100;top:-55px;} #Text88 {background-color:#fff;border:solid 0px orange;color:#222;display:none;padding:15px;} #HTML88 {z-index:350;display:none;} /* DarkLayer div */ #EPDarkLayer {background-color:#000;opacity:100%;filter:alpha(opacity=100);top:0px;left:0px;z-index:500;position:fixed;} /* Entry button */ #EPEntryButton {background-color:none;border:outset 3px none;color:#333;cursor:pointer;font: arial;font-size:25px;padding:10px;text-decoration:none;} #EPGrab {color:white;padding-top:10px;} </style> <!--[if IE 6]> <style> #EPDarkLayer {position:absolute;} </style> <![endif]--> <!-- Javascript part --> <script type="text/javascript"> YourBlogUrl="http://brennygee.blogspot.com/"; //enter your blog url here fromInternal=document.referrer.search(YourBlogUrl); //check come from where getDarkLayer=document.getElementById("EPDarkLayer").style; getText88=document.getElementById("Text88").style; getHTML88=document.getElementById("HTML88").style; if (fromInternal == -1) { //if visitor comes from external page getDarkLayer.width=screen.availWidth+"px"; //set DarkLayer width getDarkLayer.height=screen.availHeight*2+"px"; //set DarkLayer height getHTML88.display="block"; //show DarkLayer getText88.display="block"; //show message } else { //if visitor comes from internal page getHTML88.display="none"; //hide HTML gadget getText88.display="none"; //hide message } </script> <!-- Welcome page End --> Hi Guys, Quick question, is there a way to stop a JavaScript from validating after a certain point of the code. I've been "googling" like crazy and cant find a solution. I had to add a Checkbox validation last second and there was existing validation and I'm not good enough to alter all of it to reflect the checkbox part so what I did was I checked to see if at least one of the checkboxes was selected, if yes, do nothing and finish the rest of the validation. If no, prompt them with an error. The problem is after it prompts them for an error, it continues to run the rest of the validation and at the end it allows the end user to print. I cannot allow the end user to print until everything is filled in. Here is my code: Code: function validateFields() { var fieldsToVerify = [ ["PrincipalInvestigator"], ["ContactEmailAddress"], ["RFAward"], ["Sponsor"], ["AwardTitle"], ["BudgetStartDate"], ["BudgetEndDate"], ["SectionII"], ["ProjectRole"], ["EmployeeName"], ["DatesofAppointment"], ["EffortonProject"], ["Date"], ["PrintName"], ["Date2"], ["PrintName2"], ]; var emptyFields = []; var notFullFields = []; for (var i=0; i<fieldsToVerify.length; i++) { var f = this.getField(fieldsToVerify[i][0]); if (f==null) { console.println(fieldsToVerify[i][0] + " not found."); continue; } if (f.valueAsString=="") { emptyFields.push(f.name); } else if (fieldsToVerify[i].length>1 && f.valueAsString.length<fieldsToVerify[i][1]) { notFullFields.push([f.name,fieldsToVerify[i][1]]); } } for (var i in nonRequiredFieldsToVerify) { var f = this.getField(nonRequiredFieldsToVerify[i]); if (f==null) { console.println(nonRequiredFieldsToVerify[i] + " not found."); continue; } if (f.valueAsString!="" && f.valueAsString.length<f.charLimit) { notFullFields.push([f.name,f.charLimit]); } } if (this.getField("CheckBox1").value=="Yes" || this.getField("CheckBox2").value=="Yes" || this.getField("CheckBox3").value=="Yes" || this.getField("CheckBox4").value=="Yes" || this.getField("CheckBox5").value=="Yes" || this.getField("CheckBox6").value=="Yes" || this.getField("CheckBox7").value=="Yes") {} else app.alert("Select a Checkbox in Section I before printing") if (emptyFields.length==0 && notFullFields.length==0) { this.print(); } else { var msg = ""; if (emptyFields.length>0) { msg += "The following fields must be filled-in:\n"; for (var i in emptyFields) msg += emptyFields[i]+"\n"; } if (notFullFields.length>0) { if (msg!="") msg+="\n\n"; msg += "The following fields are not filled-in completely:\n"; for (var i in notFullFields) msg += notFullFields[i][0] + " (required length: " + notFullFields[i][1] + ")\n"; } app.alert(msg,""); } } How do I stop the displaymessage function when i ==1 Code: <script type="text/javascript"> var i =0; function one() { document.getElementById('control').innerHTML = 1; i =document.getElementById('control').innerHTML; } function zero() { document.getElementById('control').innerHTML = 0; i =document.getElementById('control').innerHTML; } function displaymessage() //if (i==0)//this causes problems { alert("Hello World!"); } </script> <body onclick="javascript: displaymessage();"> <div id='control' onmouseover="javascript: one();" onmouseout="javascript: zero()">0</div> Thanks to Old Pedant , in this forum, I am using this basic code for a slideshow he posted: Code: var slides = [ [ "abc.jpg", 3000 ], [ "xyz.png", 5000 ], [ "foo.gif", 1500 ] ]; var curSlide = 0; function nextSlide( ) { document.getElementById("thePicture").src = slides[curSlide][0].src; setTimeout( nextSlide, slides[curSlide][1] ); curSlide = ( curSlide + 1 ) % slides.length; } function setUp( ) { for ( var s = 0; s < slides.length; ++s ) { var image = new Image(); image.src = slides[s]; slides[s] = image; } nextSlide( ); } window.onload = setUp; which works just fine. But I can't find a way for it to play one round of the slides then stop. Could someone please help me with the right bit of code to stop the show? I know the code is a bit basic (no fades etc) but I have been unable to find a similar slideshow product that gives variable time delays for each slide. I've googled endlessly but nothing could i find.... TIA Richard ========= Hi I am new here, am having a terrible problem with animation build up. have searched and found various codes to stop,but can't implement in the .js in a way that works. Maybe am putting in wrong place. Wondering if anyone can help, would really appreciate it. Following is the code: Thank you Code: var droplinemenu={ arrowimage: {classname: 'downarrowclass', src: 'down.gif', leftpadding: 5}, //customize down arrow image animateduration: {over: 200, out: 600}, //duration of slide in/ out animation, in milliseconds buildmenu:function(menuid){ jQuery(document).ready(function($){ var $mainmenu=$("#"+menuid+">ul") var $headers=$mainmenu.find("ul").parent() $headers.each(function(i){ var $curobj=$(this) var $subul=$(this).find('ul:eq(0)') this._dimensions={h:$curobj.find('a:eq(0)').outerHeight()} this.istopheader=$curobj.parents("ul").length==1? true : false if (!this.istopheader) $subul.css({left:0, top:this._dimensions.h}) var $innerheader=$curobj.children('a').eq(0) $innerheader=($innerheader.children().eq(0).is('span'))? $innerheader.children().eq(0) : $innerheader //if header contains inner SPAN, use that $innerheader.append( '<img src="'+ droplinemenu.arrowimage.src +'" class="' + droplinemenu.arrowimage.classname + '" style="border:0; padding-left: '+droplinemenu.arrowimage.leftpadding+'px" />' ) $curobj.hover( function(e){ var $targetul=$(this).children("ul:eq(0)") if ($targetul.queue().length<=1) //if 1 or less queued animations if (this.istopheader) $targetul.css({left: $mainmenu.position().left, top: $mainmenu.position().top+this._dimensions.h}) if (document.all && !window.XMLHttpRequest) //detect IE6 or less, fix issue with overflow $mainmenu.find('ul').css({overflow: (this.istopheader)? 'hidden' : 'visible'}) $targetul.slideDown(droplinemenu.animateduration.over) }, function(e){ var $targetul=$(this).children("ul:eq(0)") $targetul.slideUp(droplinemenu.animateduration.out) } ) //end hover }) //end $headers.each() $mainmenu.find("ul").css({display:'none', visibility:'visible', width:$mainmenu.width()}) }) //end document.ready } } <input type="image" src="uploads/button_in_cart.gif" border="0" alt="Add to Cart" title=" Add to Cart " onclick="addtocart(1);"> in addtocart function I do some checking how can i change that if after some checking my variable valid=0 that it does not allow the user to submit the form? HI I have a form that has some fields and inputs . I wrote some javascript code that if there was any problem with entered values , it shows the errors but when the user press submit button , the form submited before it had shown the problems . How can halt it and showing the problems ? TNX I am using joomla, and have a tabbed menu at the top, which has different articles embedded within. I have embedded a flash video inside each article. The problem is that if i play a video and then switch to the other tab (article) without pausing the video, the video keeps on playing. And if i try to play the video in the other article, then both videos start playing. I want to stop the first video from playing as soon as I switch to the new tab. There are a lot of different files which are included in the page, so If I know where to place a particular code within the source of the final rendered webpage, I will be able to place it at the right point in a particular file. thanks the website link is: http://tinyurl.com/ycg4tcm Hi, I am using javascript function for refreshing html page. I have used [window.location.reload(true);] method to reload page from server. This code works if i use it by using Link/button etc. For Example: <input type="button" value="Refresh" onclick="window.location.reload(true);" /> But i want to refresh page when page loads. For Example: <body onload = "window.location.reload(true);> But this time, page reloads in an infinite loop. i don't know why... I have also used following function (against body onLoad) to reload and then stop, but this also does'nt work. function doLoad(){ var timeoutId = setTimeout( window.location.reload(true), 1000 ); window.clearTimeout(timeoutID); } Is there a way to stop reloading page after one refresh ? Thanks... |