How to convert danish currency to digit - javascript

I have currency like 3.477,60 kr and i want to use this currenty in js code to add extra price calculation logic. and i have used below code to format it but it give NaN in alert.
var currency = "3.477,60 kr";
var number = Number(currency.replace(/(\..*)\./g,'$1'));
alert(number);
i want output like 3477.60
can any one please guide me on this.

Here you go
var currency = "3.477,60 kr";
var number = currency.replace(/\./g, '').replace(/\,/g, '.').replace(/ kr/g,'');
alert(number);

Try this-
var currency = "3.477,60 kr";
var value = currency.replace(/\./g, '').replace(/\,/g, '.').split(' ')[0]
console.log(value);

Related

jquery convert price from 5 decimals to round up to 2 decimals

I have a price number that looks like this: $27,272.70000
I'm trying to make it look like this: $27,272.70
I'm stuck with trying different methods but here is what i've got so far:
jQuery(document).ready(function() {
jQuery('.cart-table-wrapper #shopping-cart-table tbody > tr').each(function() {
var the_sp_subtotal = jQuery(this).find('td.col-total.a-right span.price').text().replace("$", "");
var new_sp_subtotal = parseFloat(the_sp_subtotal).toFixed(2);
console.log(new_sp_subtotal);
});
});
But the result that I get is: 27.00
Here is the fiddle - https://jsfiddle.net/zqe37xsk/1/
Can someone please help me, what I'm doing wrong?
Thank you
To format a currency string properly you could use the toLocaleString function. For this to properly work you have to transform your string into a float.
var price = '27,272.70000';
price = parseFloat(price.replace(/[$,]/g, ""));
console.log(price.toLocaleString('us-EN', {
style: 'currency',
currency: 'USD'
}));
parseFloat("27,272.70") returns 27 because the , in 27,272.70 is no longer part of a number.
As an alternative approach you could replace the part behind the last thousands separator and call toFixed on that. Then you can just join everything back together.
In your each function, use:
const [dollar, ...separatedNumber] = jQuery(this).find('td.col-total.a-right span.price').text().split(/\$|,/);
separatedNumber[separatedNumber.length - 1] = Number(separatedNumber.slice(-1)).toFixed(2);
console.log("$" + separatedNumber.join(","));

How to calculate with numbers in jquery?

I am wanting to subtract some values of inputs with the total price.
The code:
$('.calculate-resterend').click(function(e) {
e.preventDefault();
var contant = $('.checkout-contant').val();
var pin = $('.checkout-pin').val();
var creditcard = $('.checkout-creditcard').val();
var waardebon = $('.checkout-waardebon').val();
var totalprice = $('.total.final-price.price').text();
alert(contant - totalprice);
});
But this returns NaN. I figure it's because total price is .text();,
but what is the correct way to substract between these things.
Lets say var contant has a value of 2000,98
and the total price has a value of 2400,99
I want it to return 400,01.
Use Number()
alert(Number(contant) - Number(totalprice));
If you also want to remove comman(,)
alert(Number(contant.replace(/\,/g,'')) - Number(totalprice.replace(/\,/g,'')));
You may have to convert one of your values using parseInt. You can try the following:
totalprice = parseInt(totalprice)
And then proceed to do your simple subtraction like before. You could also try to make your own price attribute on the HTML element itself and the fetch the attribute value instead like .attr("price"). Not sure if that would return a string as well.
try to convert your value by using parseint()
eg:- parseint(your_value)

Javascript: Most Efficient way to convert string to integer then back to string

I have a text input field which has a value of text to take in a string like "$2,000." In my functionality, I need to convert this back to a a number to run some mathematical functions and then spit it out back as another dollar value, which will be formatted like "$2,500.56" (I.E. not "$2,500.567"). Here's the two tests I've run so far:
var amount = "$2,000.58"
// "2000.58"
var amount_no_sym = amount.replace(/[^\d\.]/g, '');
//2000.58
var amount_integer = parseFloat(amount_no_sym);
//2000.58 (will cut out any additional decimal places)
var amount_decimals = amount_integer.toFixed(2);
//Final output is "$2,000.58" - the toLocaleString doesn't add back the , here?
var amount_dollar_string = "$" + amount_decimals.toLocaleString();
var amount = "$2,000.58"
// "2000.58"
var amount_no_sym = amount.replace(/[^\d\.]/g, '');
// 2000.58
var amount_integer = parseFloat(amount_no_sym);
//Final output is "$2,000.58"- but sometimes it will be something like "$3,564.345" for certain calculations.
var amount_dollar_string = "$" + amount_integer.toLocaleString();
Would the most optimal solution be to go to the second one, and then write a function to process a string and cut off the last number after the decimal if there are more than two....? Is there a simpler way and I'm doing too much work?
Thanks in advance!
Don't do your own number formatting. There's an API for that.
var formatter = new Intl.NumberFormat("en-us", { style: "currency", currency: "USD" });
console.log(formatter.format(2000.58));
In both cases you can avoid calling the function parseFloat() by using Unary + (plus) operator which attempts to convert the operand to a number, if it is not already. And to format currency you can also use Number.prototype.toLocaleString() passing as arguments the desired locale and an object with options:
var amount = '$2,000,344.58',
amount_integer = +amount.replace(/[^\d\.]/g, ''),
amount_dollar_string = amount_integer.toLocaleString('en-EN', {
style: 'currency',
currency: 'USD'
});
console.log(amount_integer);
console.log(amount_dollar_string);

Javascript mask

How do you format a number to show 2 decimals in JavaScript?
Something along the lines of:
format(x,"9,999.99");
You should use this :
x.toFixed(2);
or if you want to be sure it will work :
parseFloat(x).toFixed(2);
var num = 3.14159;
var fixed = num.toFixed(2);
If you want the commas depending on the locale:
var localed = num.toLocaleString();
Combining both crudely:
var num = 3.14159;
var fixed = num.toFixed(2);
var fixednum = parseFloat(fixed);
var localedFixed = fixednum.toLocaleString();
Use .toFixed(2):
http://www.devguru.com/technologies/javascript/17443.asp
Note this will round your number:
var i = 23.778899;
alert(i.toFixed(2));
gives you
23.78
I'm not sure if you are trying to do this to input or just for displaying text on the page. You can use the masked input plugin for jQuery if you are trying to format input.
http://digitalbush.com/projects/masked-input-plugin/

simple calculation of integer and decimal number in jquery

Trying to multiply 2 values. Quantity is integer and credit price is decimal number. When I run this code nothing happens.
Does anyone know what is the issue?
Thank you.
$(function(){ // bind the recalc function to the quantity fields
$("#oneCreditSum").after('<label></label>Total: Aud <span id=\"total\"></span><br><br>');
$("#quantity").bind('keyup', recalc);
function recalc(){
var quantity = $('#quantity').val();
var creditPrice = $('#creditPrice').val();
var total = quantity * creditPrice;
$("#total").text(total);
}});
Use parseFloat on the values, and alert each one individually to test.
A few other (unrelated) improvements:
Use keyup() function:
$("#quantity").keyup(recalc);
Make function anonymous:
$("#quantity").keyup(function(){...});
Use $(this) on #quantity in the function to avoid calling the jQuery selector again
You could also consider condensing this into a single line of code:
$("#total").text(parseFloat($('#quantity').val()) * parseFloat($('#creditPrice').val()));
To zero-pad you might try something toFixed():
var num = 10;
var result = num.toFixed(2); // result will equal 10.00
I got this snippet from the following site
http://www.mredkj.com/javascript/numberFormat.html
Hope this helps.
use
parseFloat
before calculation on both numbers which parses a string argument and returns a floating point number.
var quantity = $('#quantity').val();
var creditPrice = $('#creditPrice').val();
var total = parseFloat(quantity) * parseFloat(creditPrice);
If you are interested in whole number only you can use this function instead:
parseInt

Categories

Resources