Javascript ParseFloat to autofill without decimals - javascript

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

Related

Error in decimal division result in javascript

I'm going to do a little javascript operation on my HTML page.
I have no problem dividing 2 numbers by each other.
But if I get the same number from the html input section, the result is wrong.
Sample
total = 99.90/1.08
The result of this operation 92.50 yes I want to find this result.
But when I get the number 99.90 from input in html
total = input/1.08
The result is 91.66
How can I fix this?
function kdv_fiyat_1_1_i() {
var kdv_fiyat_1_1;
kdv = parseInt(document.getElementById('kdv').value);
fiyat = parseInt(document.getElementById('fiyat_1_1').value);
toplam = fiyat/1.08;
kdv_fiyat_1_1 = toplam
document.getElementById('fiyat_1_1').value = kdv_fiyat_1_1;
}
function kdv_fiyat_1_1_i(){
var kdv_fiyat_1_1;
kdv= parseInt(document.getElementById('kdv').value);
fiyat= parseInt(document.getElementById('fiyat_1_1').value);
toplam = fiyat/1.08;
kdv_fiyat_1_1= toplam
document.getElementById('fiyat_1_1').value = kdv_fiyat_1_1;
}
Just changed few things and I think its should work just fine.
What I did is I removed the parseInt and instead added a + operator, what this does is it converts the string to a Number, and yeah it pretty much does the job, if your logic was correct then you should get your desired output

What is the easy way to show only 4 digits after decimal point by default inside textboxes in html

I have some textboxes which are bind to decimal fields of a view model in cshtml file. Most of them have nine zeros after decimal. I want to show only 4 digits by default while editing them. However, I don't want to fix the length after decimal. Can anyone help me with this?
I suggest doing at c# controller or where you bind data to UI control like:
String.Format("{0:0.0000}", 123.456789012); // 123.4568
You can aslo do it in native javascript like :
var num = 123.456789012;
var n = num.toFixed(4); // 123.4568
var n = 56767.87488958865
console.log(n.toFixed(4)) //56767.8748
Or you can use CultureInfo in C#.
CultureInfo nfi = new CultureInfo("en-US", False);
nfi.NumberFormat.CurrencyDecimalDigits = 2;
nfi.NumberFormat.CurrencyDecimalSeparator = ".";
nfi.NumberFormat.CurrencyGroupSeparator = "";
nfi.NumberFormat.NumberDecimalDigits = 2;
nfi.NumberFormat.NumberDecimalSeparator = ".";
nfi.NumberFormat.NumberGroupSeparator = "";
//Now you can show the data like
txtValue.Text = data.ToString("N", nfi); //Format as number
txtValue.Text = data.ToString("C", nfi); //Format as currency
Maybe this will help you in the right direction

Subtract 1 from variable jQuery

Having some trouble getting this right. I'm very new to jQuery, so trying to get better and learn.
Currently I am getting 2 different values from a html table using the following code
var sellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var buyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();
These both output a value such as $13,000,000
I am then wanting to subtract 1 from these values (making it $12,999,999) before pasting them to an input as such
$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);
However, I am having some trouble with how to subtract $1 from these.
I tried using sellPrice--; but without success.
I've also tried adding - 1; at the end of each variable, but did not succeed either.
I tried to test something like this, but did not work either.
var minusOne = -1;
var getCurrentSellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var getCurrentBuyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();
var sellPrice = (getCurrentSellPrice - minusOne);
var buyPrice = (getCurrentBuyPrice - minusOne);
$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);`
Trying my best to familiarize myself with jQuery :)
Any help is much appreciated!
Solved using this
var getCurrentSellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var getCurrentBuyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();
var sellPrice = Number(getCurrentSellPrice.replace(/[^0-9\.]+/g,"")) - 1;
var buyPrice = Number(getCurrentBuyPrice.replace(/[^0-9\.]+/g,"")) + 1;
$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);
Since your numbers contain currency symbol and are strings, you need to convert them to proper numbers before subtracting them. See the answer below.
How to convert a currency string to a double with jQuery or Javascript?

Calculate total after retriveing tax rate from JSON

I have 1 select, 2 text inputs & some JSON data in a form:
select input: List of Suppliers
text input 1: Net Amount
text input 2: Gross Amount
JSON Data:contains the rates of various suppliers as JSON in supplier_tax_rates
I am calculating Gross Amount something like this(pseudo code):
grossAmount = NetAmount + ((currently_selected_supplier.tax_percentage_charged / 100) * netAmount)
Here is the complete code:
Calculate total after retriveing tax rate from JSON
Now, this should work but it doesn't. I get NaN(not a number), means something is wrong. But I have trouble find where.
JSfiddle
You have multiple problems in your code. Here is the correct version:
var taxRates = $.parseJSON(supplier_tax_rates);
var getTaxRate = function(id) {
for (var i in taxRates) { // correct loop definition
if (taxRates[i].id == id) { // check you get id correctly
return taxRates[i].tax_percentage_charged; // instead of 'rate'
}
}
};
$('#PurchaseNetAmount').on('change', function(event) {
var taxRatesId = $('#PurchaseSupplierId').val();
var netAmount = parseFloat(this.value);
var grossAmount = netAmount + ((getTaxRate(taxRatesId) / 100) * netAmount);
$('#PurchaseGrossAmount').val(grossAmount);
});
DEMO: http://jsfiddle.net/A9vmg/18/
Your problem is in the look up function.
for(TaxRate in supplier_tax_rates ){
supplier_tax_rates is a string, not a JSON object
Than after you fix that you will have another error
return rate;
What is rate?
Learn to use console.log() or breakpoints so you can step throught your code and debug it.
getTaxRate(taxRatesId) return undefined

2 text box need to verify they are not empty or contain 0

I have a function with info that grabs hours, rates, and then tax deduction and then spits it out. It works fine
var newtax= new Number(dep[i]);
taxrate = newtax*100;
var h=eval(document.paycheck.hours.value);
var r=eval(document.paycheck.payrate.value);
document.paycheck.feedback.value= taxrate + txt;
var total= r*(1-newtax)*h ;
total=total.toFixed(2);
document.paycheck.feedback3.value= ("$ "+ total);
I have to put where it takes the total and puts it in a function to put it only two decimals. It works this way and only does two decimals but i need the decimal conversion in a function. can anyone shed some like .
This is where i cut it to two decimals and i am unable to put in function and then send it back to the feedback3.value.
total=total.toFixed(2);
document.paycheck.feedback3.value= ("$ "+ total);
If you're asking how to write a function that takes a number and formats it as a dollar value with two decimals (as a string) then this would work:
function formatMoney(num) {
return "$ " + num.toFixed(2);
}
// which you could use like this:
document.paycheck.feedback3.value= formatMoney(total);
// though you don't need the total variable (unless you use it elsewhere)
// because the following will also work:
document.paycheck.feedback3.value = formatMoney( r*(1-newtax)*h );
By the way, you don't need eval to get the values from your fields. Just say:
var h = document.paycheck.hours.value;
var r = document.paycheck.payrate.value;

Categories