My code:
var $label = $(ev.currentTarget);
var $price = $label.parent("form").find(".oe_price .oe_currency_value");
if (!$price.data("price")) {
$price.data("price", parseFloat($price.text()); //price is 10.30 but it returns 10.3 as number
}
$price.html($price.data("price")+parseFloat($label.find(".badge span").text() || 0));
/* The value coming in **badge** is **12.00**
* but parseFloat converts that into **12**
* thats why after the addition I got the output as **22.3**
* but i want it as **22.30**
*/
I have a string
'10.30'
Now, if I convert the string to number using parsefloat
parseFloat('10.30')
I got the output as 10.3
And If I do it using the .tofixed(2)
parseFloat('10.30').toFixed(2)
I got the output 10.30 but it is in STRING which is the big problem for me because I want the output as number.
And if i do like this
Number(parseFloat('10.30').toFixed(2))
I got the output 10.3
But i want the output in number and with decimal point
like this 10.30
Plz help...!!!
thankz Passerby for the help.
Finally i got the result from this :
$price.html(($price.data("price")+parseFloat($label.find(".badge span").text() || 0,10)).toFixed(2));
Try this:
parseFloat(Math.round(num3 * 100) / 100).toFixed(2);
See Live Demo
I don't know what are you doing, but I've done the same and got desired result.
My code:
var str = '10.30';
var flo = parseFloat(str);
alert(flo);
flo = flo.toFixed(2)
alert(flo);
See here: Fiddle
Related
I am trying to calculate an angle in js using math js. I am experienced that when the division is between negative numbers js give me bad results. e.g. -6/-3 give me 20093 instead of 2.
How can I solve this? here below you can see a portion of console.log.
Here is the code:
var num = math.eval(parseInt(p[1]) - parseInt(d3.event.y));
var den = math.eval(parseInt(p[0]) - parseInt(d3.event.x));
if (den==0){
var angle = 0;
}else{
var m = math.eval(num/den);
if(m<1){
theta = m*100;
}else{
theta = m*100;
}
}
Syntax in code is num/den as you can see.
Thanks in advance
You can simply do -6/-3.
Run this in your developer tools console: alert(-6/-3); and you will see.
I think math.eval() expects a string like "-6/-3".
I have some inputs in a HTML form using javascript to autofill the inputs on changes
The problem is that the follow code show decimals longer decimals like this 42880.34623465464356435634 The code is:
thetotal.value = parseFloat(total.value) * (1.00/1.19);
This code get the price less taxes from the total and it works, but it show me the decimals...
What I need is the price to be in the example:
42880
Without dots or commas . or , like 42.880 or 42,880 and absolutly not 42880.34623465464356435634
I only want to show 42880
Tried with .round like this:
thetotal.value = parseFloat(total.value) * (1.00/1.19).round(2);
.round(2) but for some reason the addEventListener stop working for this input when using .round(2)
user parseInt function for it. Like below :
thetotal.value = parseInt(parseFloat(total.value) * (1.00/1.19));
You will get only Integer as output.
Another way is Math.round. Like it below :
thetotal.value = Math.round(parseFloat(total.value) * (1.00/1.19));
why don't you use Math.round()?
var total = {};
total.value = 51027.6120192;
var thetotal = {};
thetotal.value = Math.round(parseFloat(total.value) * (1.00/1.19));
document.write(thetotal.value);
Output
42880
My current solution does not calculate the added ship cost. My condition isn't working, I've been able to do similar problems with functions but I would like to know where I went wrong here.
<html>
<script type="text/javascript">
var numOrdered = 0;
var priceItem = 0;
var shipCost = 0;
var amtDue = 0;
numOrdered = prompt("Enter the number ordered ",0);
priceItem = prompt("Enter the price of the item ",0);
amtDue = numOrdered * priceItem;
if (amtDue > 1000)
{
shipCost = 0;
}
else
{
shipCost = amtDue * .1;
}
amtDue = amtDue + shipCost;
document.write("You owe ", amtDue);
</script>
</html>
It works for me. I put your code here: http://jsfiddle.net/XVzKb/
No changes -- if I enter 5 and then when prompted again 10 the total displayed in the page is 55 which is 5 * 10 * 1.1 = 55. Note that if your input includes a non-numeric character like $ or , then you'll need to parse it like so:
number = parseInt(response.replace(/[^0-9]/g, ''), 10)
I would check out the developer tools in Chrome or Firefox and use console.log or put in some break points to help you understand what is working and what is not. This way you can see if its your javascript or your html document that is failing.
That being said, you are comparing strings not numbers, so you'll want to use parseInt on the values you are getting back from the prompt.
priceItem = parseInt(priceItem, 10);
im trying to find if there is any javascript to format a string for display, so for exam "1234"- or any string over length 2- would become 12** i know there is a replace method but not sure how this would work. any suggestions welcome. thanks much
Assuming you want to mask the equivalent number of characters you can replicate * length - 2 times & append the 1st 2 characters of the original string;
var str = "123456";
var numCharsToKeep = 2;
if (str.length > numCharsToKeep)
str = str.substr(0, numCharsToKeep) + Array(str.length - numCharsToKeep + 1).join("*")
== "12******"
I'm trying to get the percentage between two numbers for the purpose of showing the difference as far as a discount. I've tried to simplify it as much as I can but I still cant get what I want.
Here is an example.
var RegPrice = 8.95;
var OnSale = 6.67;
var OnSaleAT = Math.abs(Math.max(100.00 - OnSale / RegPrice * 100.00));
alert(OnSaleAT.toFixed(2));
What I'm trying to get is the alert(); to return a value of 25.50. However, I'm getting 25.47.
Any ideas on how I can get this right?
25.47486... is the correct answer. If you're attempting to round to the nearest tenths, you can use:
var result = Math.round(OnSaleAT * 10) / 10;
Which outputs: 25.5 and from there you can format your answer how you like.
$(window).load(function() {
var RegPrice = 8.95;
var OnSale = 6.67;
var OnSaleAT = Math.round(Math.abs(Math.max(100.00 - 6.67 / 8.95 * 100.00)) * 10.00) / 10.00
alert(OnSaleAT.toFixed(2));
});
If you're trying to round to 25.5%, you can just alert with a toFixed param of 1 instead of 2:
alert(OnSaleAT.toFixed(1));
Other than that, Rob W is right...the math does come out to 25.47, and there's not much you can do about that.
try OnSaleAT.toFixed(1)+'0' :)
Math.ceil(1000* (1 - OnSale/RegPrice))/10
give you "25.5"
:)