Sum code not working properly [duplicate] - javascript

This question already has an answer here:
Function sum not working properly javascript [duplicate]
(1 answer)
Closed 7 years ago.
I'm trying to redirect from www.domain1.com to www.domain2.com getting the actual domain name.
Let's say that our page is www.domain1.com:
var domainName = window.location.host;
var domainNumber = domainName.substr(10, 1);
var finalDomain = (domainNumber+1);
If I print finalDomain on the screen, I got 11. So because of that my redirect is not working.
How do I do domainNumber plus 1, so I got 2 and not 11?

substr method will return you a string, not a number. so you are getting "1" , not 1
when you do "1"+1, you will get "11"
use parseInt() method to convert "1" to 1 before the addition operation.
var finalDomain = parseInt(domainNumber)+1;

domainNumber is a string, not a number, so when you add 1, you're actually concatenating the string "1" onto the end of a string "1" to make "11". You need to convert it into a number using the parseInt() function, as follows:
var domainNumber = parseInt(domainName.substr(10, 1), 10);
Note: The second parameter passed to parseInt() signifies that the string is in decimal (base 10). Now domainNumber is a number rather than a string, so if you try to add one to it you'll get 2.
var finalDomain will be a number, unless you set it to be a string by calling toString():
var finalDomain = (domainNumber + 1).toString() - If you do this, finalDomain will be a string containing the value "2".
Hope this helps.

This function should do the trick parseInt:
Converts a string to an integer.
var finalDomain = parseInt(domainNumber) + 1;
The issue you are having is because in javascript "1" + 1 is "11". So, you need to convert the string "1" to int in order to add it 1 afterwards.
Test it yourself here:
alert("1" + 1 === "11")
alert(1 + 1 === 2)

try this
var domainName = window.location.host;
var domainNumber = domainName.substr(10, 1);
domainNumber = parseInt(domainNumber);
var finalDomain = (domainNumber+1);
u need to parse ur domainNumber variable to integer type in order to do addition.
see here http://www.w3schools.com/jsref/jsref_parseint.asp

Related

Convert string to hex then back to string

I have to convert a working C# function to JavaScript so it executes client-side. Here's the C#...
// convert the cmac into a hex number so we can increment it and get the emac
long emacLong = Convert.ToInt64(_cmac, 16) + 1;
emac = emacLong.ToString("x12").ToUpper();
Here's what I have so far in JavaScript..
var emac = parseInt(cmac, 16) + 1;
emac = emac.toString(16);
The input is "0015D1833339". The output should be "0015D183333A". However, the JavaScript returns "15d183333a". I need to retain the leading 0's. Looks like the C# function accomplishes this with the "x12" parameter of .ToString. How do I accomplish this in JavaScript? I need to convert it to an integer, increment it by 1 and then convert back to a string with a length of 12 characters.
Easy way to pad hex number output when you know the exact length you desire is with something like this:
var emac = parseInt(cmac, 16) + 1;
emac = ("000000000000" + emac.toString(16)).substr(-12);
// or if you MUST have all caps....
emac = ("000000000000" + emac.toString(16)).substr(-12).toUpperCase();
This example is for length 12, if you need a different length, you would adjust the length of the 0 string and the substr param.

How do I write an equation inside the following javascript brackets?

In my angular.js controller I want to select the array number that matches $routeParams.propertyID by passing it in. The below works:
$scope.selectedProperty = $scope.all_properties[$routeParams.propertyID];
BUT with arrays, 1 = 0, so it selects the previous array that I want. How would I pass in +1 to select the correct array? I have tried the following with no luck:
$scope.selectedProperty = $scope.all_properties[$routeParams.propertyID + 1];
$scope.selectedProperty = $scope.all_properties[($routeParams.propertyID + 1)];
$scope.selectedProperty = $scope.all_properties[($routeParams.propertyID) + 1];
Cheers
You property is probably a string, try add a parse to int before doing math
If you are doing
var calc = "1" + 1 // calc = "11"
If you are not sure, add a console.log($routeParams.propertyID) and check you're firebug console
$scope.selectedProperty = $scope.all_properties[parseInt($routeParams.propertyID,10) + 1];
And if you are reading at pos one instead of pos zero, you should substract
$scope.selectedProperty = $scope.all_properties[parseInt($routeParams.propertyID,10) - 1];
if propertyID is a string, you probably want to convert it to a number for the addition
$scope.selectedProperty = $scope.all_properties[+$routeParams.propertyID + 1];
or, if Brandon's comment is correct
$scope.selectedProperty = $scope.all_properties[+$routeParams.propertyID - 1];
Note in both cases the plus sign (+) before the string that converts it to a number
There are two issues in your context:
First issue: add or subtract
Why are you adding one? Shouldn't you be subtracting one?
If array's zero corresponds to ID's one, then you must do "minus one":
+$routeParams.propertyID - 1
Second issue: string or number
String or number?
Maybe your ID is a string while the array index is a number. In this case, this should work:
$scope.selectedProperty = $scope.all_properties[+$routeParams.propertyID - 1];
But, if you say your first code works, perhaps you don't have an "array", but a hash (or "dictionary").
In this case, after adding or subtracting one, you must convert the result back to string:
$scope.selectedProperty = $scope.all_properties[(+$routeParams.propertyID - 1) + ""];

get wrong result function jquery javascript

I am using the following script. But I am receiving a wrong result for x_b_bbetrag.
When do an calculation exp 100/108 I get 9.92 instead of 92.59.
What am I missing here?
Code below:
var betrag = 100
var kurs = 1
var minkl= 1
var msatz= 0.08
$("#x_b_betrag").change(function() {
var betrag = $("#x_b_betrag").val();
var kurs = $("#x_b_kurs").val();
var minkl =$("input[name='x_b_mwstinkl']:checked").val();
var msatz =$("input[name='x_b_mwst']:checked").val();
if (minkl == "1"){
$("#x_b_rechenbetrag").val((betrag * kurs).toFixed(2));
$("#x_b_bbetrag").val( ( (betrag * kurs) /(1 + msatz) ).toFixed(2));
}
Use parseFloat
multiplication, division and subtraction automatically parse string to number. for summation you need to parse it.
$("#x_b_bbetrag").val( ( (betrag * kurs) /(1 + parseFloat(msatz) ) ).toFixed(2));
///1 + "1" = 11 not 2
Parse your inputs into numbers.
For example :
var betrag = parseFloat($("#x_b_betrag").val());
MDN on parseFloat
The value of the msatz variable is not 0.08 but "0.08". It's a string, so when you add one to it, the number will be converted to a string so that they can be concatenated, and the result is "10.08" not 1.08. The string will implicitly be converted to a number when you use it in the division, as it's not possible to divide by a string.
Parse the string into a number:
var msatz = parseFloat($("input[name='x_b_mwst']:checked").val());

Convert String to Float in Javascript error

Please see below code,i getting wrong value.
eg;
var FirstValue=0.00;
var secondvalue=parseFloat("22.88",10).toFixed(2);
var thirdvalue=(FirstValue) + (secondvalue);
am getting value like"22.8822.88"
Please help me to solve.Its not convert to numeric .
toFixed convert you float value to string back. So when you adding two values you will get not number addition but string concatenation:
2.0 + 2.0 = 4.0 // number
"2.0" + "2.0" = "2.02.0" // string
Remove to fixed after conversion. Than add two values and than do to fixed:
var FirstValue=0.00;
var secondvalue=parseFloat("22.88",10);
var thirdvalue= ( (FirstValue) + (secondvalue) ).toFixed(2);
try this:
var FirstValue=0.00;
var secondvalue=parseFloat("22.88");
secondvalue = parseFloat(secondvalue.toFixed(2));
var thirdvalue=(FirstValue) + (secondvalue);
toFixed returns a string, not a number so it needs to be converted again.

How to append an extra 'Zero' after decimal in Javascript

Hye,
Iam new to javascript working with one textbox validation for decimal numbers . Example format should be 66,00 .but if user type 66,0 and dont type two zero after comma then after leaving text box it should automatically append to it .so that it would be correct format of it . How can i get this .How can i append ?? here is my code snippet.
function check2(sender){
var error = false;
var regex = '^[0-9][0-9],[0-9][0-9]$';
var v = $(sender).val();
var index = v.indexOf(',');
var characterToTest = v.charAt(index + 1);
var nextCharAfterComma = v.charAt(index + 2);
if (characterToTest == '0') {
//here need to add
}
}
Use .toFixed(2)
Read this article: http://www.javascriptkit.com/javatutors/formatnumber.shtml
|EDIT| This will also fix the issue if a user types in too many decimals. Better to do it this way, rather than having a if to check each digit after the comma.
.toFixed() converts a number to string and if you try to convert it to a float like 10.00
then it is impossible.
Example-
10.toFixed(2) // "10.00" string
parseFloat("10.00") // 10
Number("10.00") // 10

Categories

Resources