I am trying to do a simple thing but for some reason it is not working. I am using Knockout and I have a model which I update after user enter some data and use the same to communicate back to C# code on server side. For some reason, when I try to assign decimal value to one of the member of model it isn't working. Though, in this case I am using knockout, I believe it has nothing to do with KO. See the screenshot where I have the value 22.78 and I am trying to do parseFloat but it ends up as just 22. I tried other things such as removing he parseFloat just to see if it accepts the string value as it is but even that is not working. Can someone help?
Your doing Bitwise OR while assigning the value.
this.AMOUNT_RECEIVED = parseFloat(data.AMOUNT_RECEIVED) | 0;
so only it returns 22. Because 22.78 | 0 is 22.
Please check this code.
console.log(22.78 | 0);
Please try this code while assigning value. You can get decimal values without loss.
this.AMOUNT_RECEIVED = parseFloat(data.AMOUNT_RECEIVED);
Pelase Check below link for more details.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_OR
try this (please notice the double || )
this.AMOUNT_RECEIVED = parseFloat(data.AMOUNT_RECEIVED) || 0;
Related
I came across this pen when searching for a way to make a textarea auto-expand with input: https://codepen.io/vsync/pen/frudD. The code includes the line
var minRows = elm.getAttribute('data-min-rows')|0, rows;
I do not understand what the bitwise or and 0 after getting the dataset attribute does. I tried removing them, but that breaks the code.
Also, I think that the ", rows" is a remnant from a previous version of the code as "rows" is not defined until later in the code, and I can remove that and the code still appears to work (please correct if I am missing something).
The bitwise OR and 0 serves to convert the string property into a number to work properly. This can be verified by checking the type of the variable.
Usually, the stuff after the comma is used as the result, but since it is undefined, whatever comes in front is used instead (I think).
I've got several input fields and such jquery code:
jQuery(document).ready(function($){
$('input').change(function(){
var amount1=$('.product-addon-wesprzyj-autora input').val();
var amount2=$('#pa_kategoria-cenowa').val();
var amount3=$('.quantity .qty').val();
var fractal=0.01;
var Total=(amount1*amount2*fractal*amount3)+(' PLN');
$('.product-addon-kwota-dla-autora input').attr('value', Total);
});
});
Strange thing is that jquery code works. In real time in changes values in my inputs, BUT after clicking sumbit, value from '.product-addon-kwota-dla-autora input' seems to be empty in database. Why is that? is there any way of pushinig this code to work? If i will type via keyboard data into that field, everything works. ANy ideas what is here wrong?
With the code you provided, I am not quite sure what your problem is. It could be a problem in your server side code where you save the data to the table. You need to debug and that find out.
But make sure that you are setting the form values properly so that your server side code will have the correct data from the form. You may consider using the val() method to set value.
$('.product-addon-kwota-dla-autora input').val(Total);
Also , when you read values from form inputs and use it for numeric operations, Consider converting the type to a numeric version by using parseFloat() or parseInt()
var Total=parseFloat(amount1)*parseFloat(amount2)*parseFloat(fractal)
*parseFloat(amount3)+(' PLN');
Also, You may inspect your browser's network tab to see what data your browser is posting to the server code. That should help you to understand where your problem is.
it might me that your "Total" variable is empty. alert('Total') and check for result. if it shows some value then try to change your line from this
$('.product-addon-kwota-dla-autora input').attr('value', Total);
into this
$('.product-addon-kwota-dla-autora input').val(Total);
I'am experimenting with selenium IDE and i came across a problem with asserting an approximate value. I need to check a value inside an element with an id. It is numeric value with comma (",") as a separator.
Problem is that i need to check if the numeric value is valid with a tolerance of 0.01.
For example:
<div id="uniqueId">2,54</div>
assertText - value = 2.53
I need above example to pass the test, and also pass if the value in div si 2,52 or 2,53. I understand that i can use assertEval to insert javascript, but i'm not very good in javascript and also from what i've read the javascript capabilities of selenium are limited.
Any help would be greatly appreciated!
Using assertEval is a good idea. The javascript you will need will be something like
var numberStr = "${actualText}".replace(",", ".");
var number = parseFloat(numberStr);
var difference = Math.abs(eval(number-${expectedValue}));
(difference <= 0.01)?true:false;
I don't know much javascript but according to this thread we need to first replace decimal mark from ',' to '.' (1st line) so we can later convert the string found on page to number (2nd line).
${actualText} is a variable in which we store the actual value taken from page while the ${expectedValue} is a value you need to define on your own. Note that tolerance (0.01) is "hardcoded", you may want to replace it with variable too.
Now to make it shorter (and less readable):
(Math.abs(eval(parseFloat("${actualText}".replace(",", "."))-${expectedValue}))<=0.01)?true:false
Having the javascript we can prepare Selenium script:
storeText | id=uniqueId | actualText
store | 2.53 | expectedValue
assertEval | JS LINE FORM ABOVE GOES HERE | true
This is by far the strangest error I've ever seen.
In my program I have one variable called avgVolMix. It's a decimal variable, and is not NaN (console.log(avgVolMix) prints something like 0.3526246 to console). However, using the variable at all in an assignment statement causes it to corrupt whatever is trying to use it to NaN. Example:
console.log(avgVolMix); <- prints a working decimal
var moveRatio = 10 + avgVolMix * 10;
console.log(moveRatio); <- prints NaN
I seriously have no idea why this is happening. I've tried everything to fix it; I've converted it to a string and then back, rounded it to 2 decimal places, adding 0.0001 to it - nothing works! This is the only way I can get it "working" right now:
var temp = 0.0;
for(i = 0; i <= avgVolMix; i+=0.1)
temp = i;
This assigns a number that is close to avgVolMix to temp. However, as you can see, it's extremely bad programming. I should also note that this isn't just broken with this one variable, every variable that's associated with a library I'm using does this (I'm working on a music visualizer). Does anyone know why this might be happening?
Edit: I'm not actually able to access the code right now to test any of this stuff, and since this is a company project I'm not comfortable opening up a jsfiddle anyway. I was just wondering if anyone's ever experienced something like this. I can tell you that I got the library in question from here: http://gskinner.com/blog/archives/2011/03/music-visualizer-in-html5-js-with-source-code.html
If its showing the variable value as NaN. Then try converting the variable as parseInt(); method. Hope it works. Because I also faced such problem and solved when tried it.
This is a follow up question to this:
Google Analytics Event Tracking via a jQuery plugin
Matt Austin was correct. I was passing a string of the integer for the GA value parm when I should have been passing the int value itself. Evidently Google Analytics is sensitive like that :)
So I changed: parmValidatedObject[key] = val;
to: parmValidatedObject[key] = val.valueOf();
But that doesn't seem to be working as expected. GA doesn't seem to pick this up as an int. What am I not understanding about valueOf()?
btw, I might also run into something similar with one of the other parms that's a boolean. I'm translating those strings to the value or 0 or 1 but I'm wondering (out loud and in advance) if that's the right approach.
parseInt(val,10); will convert your string to an integer. It is a JavaScript function, not jQuery.
!!val converts a value to a boolean. Note that "0" is considered a truthy value and so !!"0" returns true, unlike the false you might expect. In this case, you may be better off using
(""+val == "0") ? false : true;