How to pass complete value through parseint () method - javascript

i am working with parseInt() as it only allow to pass value before decimcal place how i could pass the whole value i need to add the value in the textboxes .this below is code for the display of result after getting value from three different textbox using javascripting
function getText6(){
var in32=document.getElementById('in3').value;<value from textbox1>
var in332=document.getElementById('in33').value;<value from textbox2>
var in3332=document.getElementById('in333').value;<value from textbox3>
var in123=parseInt(in32)+parseInt(in332)+parseInt(in3332);
document.getElementById('in123').value=in123.toFixed(2);
}
now when in textbox in123 value come only take two digits before decimal plce.
like 34.56+138.00+964.80=1136.00 it take only 34+138+964=1136 i want that it take whole term and ans =1137.36
kindly help me for this result

Use parseFloat instead of parseInt. The latter strips away any decimal places since integers do not have them.

use parseFloat() instead of parseInt()
var in123=parseFloat(in32)+parseFloat(in332)+parseFloat(in3332);

Related

In AutoNumeric javascript, how do I create a format that shows integer if the input is an integer but currency if the input is a float?

I need to create a format in AutoNumeric javascript that does the following:
If the textbox's input value is 87, leave it unformatted: 87
If the input value is 87.6, show it as: 87.60
If the input value is 87.65, show it as: 87.65
If the input value is 87.654, show it as: 87.654
If the input value is 87.6543, round it down or truncate it: 87.654
If the input value is 87.65432, again, round it or truncate it to 87.654
I have it all worked out except for the situation where the input value is 87.6. The formatted value remains 87.6, but I want it to be 87.60, because it's a currency that can have 2 or more decimal places, but never just 1.
If you do those manipulations in pure JavaScript you can use Number.prototype.toFixed
(23.2).toFixed(2); // 23.20
Not sure what you mean by 'I have to use AutoNumeric' -- perhaps you could elaborate in your original post to clarify?
Otherwise, you can use regex to check for a single decimal and then use Number.toFixed() to make sure it has 2 decimal points if there's only one.
let v = 87.6
var regexp = /^\d+\.\d{1}$/;
// returns true
if (regexp.test(v)) {
v = v.toFixed(2)
}

Trouble converting javascript string to number

I have a script assigns a variable using parseFloat as follows:
var vendorCost = parseFloat(vendorSearchresults[0].getValue('vendorcost')).toFixed(2);
I assumed this would make the variable a number. However when I check it with typeof - it reports it as a string. My solution is as follows:
vendorCost = parseFloat(vendorCost);
Which works, however I'm trying to be more efficient when coding and would like to understand why it doesn't make vendorCost a number when assigning it a number? Is there a way I could make the first statement make vendorCost a number without the need for the second statement? Thanks in advance.
Update - just thought I should mention I'm having the exact same issue without using .toFixed -
var vendorLandedCost = parseFloat(vendorSearchresults[0].getValue('custentity_asg_landed_cost','vendor'));
vendorLandedCost = parseFloat(vendorLandedCost);
The last toFixed() call converts the result of the first parseFloat into a string.
Looks like you need to round the number to two decimal places, which is why you're using the parseFloat call. You can do something like this instead:
vendor_cost = Math.round(parseFloat(vendorSearchresults[0].getValue('vendorcost')) * 100) / 100
Well, Number.toFixed returns string because it is a data presentation function.
See the docs.

Read a number value from a div element and round to 2 decimal places

I want to retrive a number value from a div and then round that number to 2 decimal places using jQuery.
So far I have the element name and value:
<div class="value">Price = £133.3223443</div>
I am using the toFixed() method to convert a number into a string then round to 2 decimal places:
var inNum = 12345.6789;
inNum.toFixed(2);
However, I am having trouble trying to read a number within the element on the page (ignoring 'Price'), rather than just rounding a number entered within the jQuery.
Parse it with regexp? :)
http://jsfiddle.net/LERFB/2/ <--- working fiddle
var price = $('div.value').text();
var parsedPrice = parseFloat(price.replace(/([^0-9\.])/g, ''));
alert(parsedPrice.toFixed(2));
You can use text() to get the value from the element, and the split() by £ to get the numerical price. Try this:
var inNum = parseFloat($('.value').text().split('£')[1]).toFixed(2);
Example fiddle
Obviously you will also need some form of verification to ensure that there is a £ character in the string to split by, and that the value retrieved is numerical.
The following regular expression will extract the floating numbers from the string and do a toFixed operation.
var inNum = parseFloat($(".value").text().match(/[\d\.\d]+/i)).toFixed(2);
Please make sure you have the value inside the container all the time.

JavaScript: Retrieve negative floating point number from string of mutiple sums

I have a simple app that allows me to caculate the total amount invoiced and deposited in a route. However I want to allow the user to input multiple values in a single input field; e.g:
500+50+36.5-45.2-10.
I have written a function that will retrieve this input and then split this string into elements of an array at the + sign and immediately parse the values to numbers and then add them and return the total the user inputs into each individual field. This is all well as long as the user does no use any sign other than +.
I have searched the use of regexp:
regular expression in javascript for negative floating point number result stored in array
Javascript Regular expression to allow negative double value?
but none of the results seem to work.
How could I make my code retrieve the values so that the negative values get passed into the array as negative values?
Here is a snippet of my Js code:
For the full code, visit my fiddle.
totalInvoiced: function () {
var a = A.invoiced.value;
var value1Arr = [];
value1Arr = a.split("+").map(parseFloat);
var value1 = 0;
value1Arr.forEach(function (value) {
value1 += value;
});
I really like PhistucK's solution, but here an alternative with regex:
value1Arr = a.split(/(?=\+|\-)/);
This will split it, but keeps the delimiter, so the result will be:
["500", "+50", "+36.5", "-45.2", "-10"]
A bit dirty, but maybe a.replace(/-/g, "+-").split("+"), this way, you add a plus before every minus, since the negative numbers just basically lack an operator.
You can use this pattern that splits on + sign or before - sign:
var str = '500+50+36.5-45.2-10';
console.log(str.split(/\+|(?=-)/));

Javascript convert string to integer

I am just dipping my toe into the confusing world of javascript, more out of necessity than desire and I have come across a problem of adding two integers.
1,700.00 + 500.00
returns 1,700.00500.00
So after some research I see that 1,700.00 is being treated as a string and that I need to convert it.
The most relevant pages I read to resolve this were this question and this page. However when I use
parseInt(string, radix)
it returns 1. Am I using the wrong function or the an incorrect radix (being honest I can't get my head around how I decide which radix to use).
var a="1,700.00";
var b=500.00;
parseInt(a, 10);
Basic Answer
The reason parseInt is not working is because of the comma. You could remove the comma using a regex such as:
var num = '1,700.00';
num = num.replace(/\,/g,'');
This will return a string with a number in it. Now you can parseInt. If you do not choose a radix it will default to 10 which was the correct value to use here.
num = parseInt(num);
Do this for each of your string numbers before adding them and everything should work.
More information
How the replace works:
More information on replace at mdn:
`/` - start
`\,` - escaped comma
`/` - end
`g` - search globally
The global search will look for all matches (it would stop after the first match without this)
'' replace the matched sections with an empty string, essentially deleting them.
Regular Expressions
A great tool to test regular expressions: Rubular and more info about them at mdn
If you are looking for a good tutorial here is one.
ParseInt and Rounding, parseFloat
parseInt always rounds to the nearest integer. If you need decimal places there are a couple of tricks you can use. Here is my favorite:
2 places: `num = parseInt(num * 100) / 100;`
3 places: `num = parseInt(num * 1000) / 1000;`
For more information on parseInt look at mdn.
parseFloat could also be used if you do not want rounding. I assumed you did as the title was convert to an integer. A good example of this was written by #fr0zenFry below. He pointed out that parseFloat also does not take a radix so it is always in base10. For more info see mdn.
Try using replace() to replace a , with nothing and then parseFloat() to get the number as float. From the variables in OP, it appears that there may be fractional numbers too, so, parseInt() may not work well in such cases(digits after decimal will be stripped off).
Use regex inside replace() to get rid of each appearance of ,.
var a = parseFloat('1,700.00'.replace(/,/g, ''));
var b = parseFloat('500.00'.replace(/,/g, ''));
var sum = a+b;
This should give you correct result even if your number is fractional like 1,700.55.
If I go by the title of your question, you need an integer. For this you can use parseInt(string, radix). It works without a radix but it is always a good idea to specify this because you never know how browsers may behave(for example, see comment #Royi Namir). This function will round off the string to nearest integer value.
var a = parseInt('1,700.00'.replace(/,/g, ''), 10); //radix 10 will return base10 value
var b = parseInt('500.00'.replace(/,/g, ''), 10);
var sum = a+b;
Note that a radix is not required in parseFloat(), it will always return a decimal/base10 value. Also, it will it will strip off any extra zeroes at the end after decimal point(ex: 17500.50 becomes 17500.5 and 17500.00 becomes 17500). If you need to get 2 decimal places always, append another function toFixed(decimal places).
var a = parseFloat('1,700.00'.replace(/,/g, ''));
var b = parseFloat('500.00'.replace(/,/g, ''));
var sum = (a+b).toFixed(2); //change argument in toFixed() as you need
// 2200.00
Another alternative to this was given by #EpiphanyMachine which will need you to multiply and then later divide every value by 100. This may become a problem if you want to change decimal places in future, you will have to change multiplication/division factor for every variable. With toFixed(), you just change the argument. But remember that toFixed() changes the number back to string unlike #EpiphanyMachine solution. So you will be your own judge.
try this :
parseFloat(a.replace(/,/g, ''));
it will work also on : 1,800,300.33
Example :
parseFloat('1,700,800.010'.replace(/,/g, '')) //1700800.01
Javascript doesn't understand that comma. Remove it like this:
a.replace(',', '')
Once you've gotten rid of the comma, the string should be parsed with no problem.

Categories

Resources