javascript toFixed(2) not working - javascript

I have this piece of code:
var desconto = document.getElementById('desconto').value;
var portes = document.getElementById('portes').value;
var pr_equipamentos = document.getElementById('pr_total_equipamentos_escondido').value;
var pr_total;
pr_total = (pr_equipamentos * ((100-desconto)/100)) + portes;
pr_total = pr_total.toFixed(2);
alert(pr_total);
document.getElementById('pr_total_proposta').innerHTML = pr_total + " €";
The ID's desconto, portes and pr_total_equipamentos_escondido are input type in a form.
In this case I'm not able to use the toFixed(2). The first formula of pr_total gives me the number: 1324.7865372846 and the next step is not working (pr_total = pr_total.toFixed(2)).
What am I doing wrong?

When you define a var, in JavaScript it is a string, so you need to parse.
try:
pr_total = parseFloat(pr_total).toFixed(2);

_txt3.value = (parseFloat(t1) - parseFloat(t2));
_txt3.value = parseFloat(_txt3.value).toFixed(2);

Related

Input field values not arithmetically summing up

Goodday, please i have a code to calculate the efficiency of a generator. The problem is the input fields all add up until the last variable. If all values were 2+2+3+4 which normally sums up into 11 normally, this program doesn't do that instead it just adds the 4 as in 2+2+3+4 equals 74.
That's the formula for calculating the efficiency of a generator.
$('.efmit').on('click', function efficiency() {
var vI = $('.I').val();
var vV = $('.V').val();
var ia = $('.ia').val();
var If = $('.If').val();
var Ra = $('.Ra').val();
var closs = $('.closs').val();
var vi_combo = vI*vV;
var ias = (ia*ia)*Ra;
var iv = If*vV;
var cent = 100;
var result = vi_combo+ias + iv;
var finalR = result + closs;
window.alert(finalR);
})
jQuery val method like $('.closs').val() returns String type variable not Number type.
You can cast type of variable to solve the problem.
var closs = Number($('.closs').val());
The reason is your program treated your variable as a string
try converting them to integer by parsing them like this parseInt(yourVariable).

Is there a way to use parameterized queries with this code?

I've gotten this from the forum already, but one of the answers provided a way to use search parameters in a url string. Some of my tables are too big to load in a browser, so apparently I will have to find out how to add search parameters to this code. The only reason I didn't use the other code was that it showed how to do that with a calendar.
I know nothing about JSON/jQuery/razor/c#. Please help.
#{
var db = Database.Open("LGOnline");
var result = db.Query("SELECT * FROM CashOS");
var data = result.Select(x => new
{
ID = x.ID,
STORE_NO = x.STORE_NO,
DATE = x.DATE,
MWS_AMOUNT = x.MWS_AMOUNT,
FINAL_AMOUNT = x.FINAL_AMOUNT
}).ToArray();
Json.Write(data, Response.Output);
Response.ContentType = "application/json";
}
Update: I am using Microsoft WebMatrix 3. I am able to get the data from my tables with this code and it converts them to JSON. I just don't need all of it at once.
Update: I got it to do what I was trying to do. It isn't pretty, and I am open for any number of suggestions, but this made it to where I can type in the url and add &STORE_NO=55 and also i can select a date if I want to.
#{
var db1 = Database.Open("LGOnline");
var formValue2 = Request.Form["STORE_NO"];
var formValue3 = Request.Form["DATE"];
if (IsPost)
{
Response.Redirect("test.cshtml?&STORE_NO=" + formValue2 + "&DATE=" + formValue3);
}
var Keyword2 = Request.QueryString["STORE_NO"]; //Retrieves passed variable from the database search page for STORE_NO
var Keyword3 = Request.QueryString["DATE"]; //Retrieves passed variable from the database search page for DATE
var sqlQ = "SELECT * FROM CashOS WHERE STORE_NO LIKE #0 AND DATE LIKE #1";
var dataQ = db1.Query(sqlQ, "%" + Keyword2 + "%", "%" + Keyword3 + "%");
var requestedData = dataQ.Select(x => new
{
ID = x.ID,
STORE_NO = x.STORE_NO,
DATE = x.DATE,
MWS_AMOUNT = x.MWS_AMOUNT,
FINAL_AMOUNT = x.FINAL_AMOUNT
}).ToArray();
Json.Write(dataQ, Response.Output);
Response.ContentType = "application/json";
}
This is the output by the way:
[{"ID":28,"STORE_NO":55,"DATE":"/Date(1442811600000)/","MWS_AMOUNT":10.1600,"FINAL_AMOUNT":10.1600}]
Thank You all for your help!!

javascript: get hex from long url

For example i have such urls:
https://portalvhdsk7w6i7k9.blob.core.windows.net:443/project/5633cc12da73d9160c4cf146.dat
https://portalvhdsk7w6i7k9.blob.core.windows.com/project/5633cc12da73d9160c4cf146.dat
https://example.com/project/5633cc12da73d9160c4cf146.dat
can i somehow get only 5633cc12da73d9160c4cf146 using only js? is it possible?
You can split the string by / and the pop() the final element. Try this:
var url = 'https://portalvhdsk7w6i7k9.blob.core.windows.net:443/project/5633cc12da73d9160c4cf146.dat'
var hex = url.split('/').pop().replace('.dat', ''); // = '5633cc12da73d9160c4cf146'
If you want to get the location of the current page, use document.location.
Check this:
var url = "https://portalvhdsk7w6i7k9.blob.core.windows.net:443/project/5633cc12da73d9160c4cf146.dat"
var indexofProd = url.indexOf("project/");
indexofProd = indexofProd + 8;
var indexofdotDat = url.indexOf(".dat");
var number = url.substring(indexofProd,indexofdotDat);
alert(number);

Manipulation data on javascript

I have some codes like this
var f = document.frmR5B075;
var str = f.cbo_BilServProvCodeHidden.value;
var afterDash = str.substr(str.indexOf("-") + 1);
And i want put afterDash value back to f.cbo_BilServProvCodeHidden.value. I tried use f.cbo_BilServProvCodeHidden.value = afterDash; seems not working / cannot get the value. Any Solution ? Thanks
f.cbo_BilServProvCodeHidden.value = afterDash seems to work for me.
Here is the demo

Having trouble converting user input into a graphic bar

The code seems to work fine when inputting numbers 1-9 but anything above doesn't work, what could be the issue? Here is the code:
var varkString = prompt('Enter your VARK scores - [visual|aural|read|kinesthetic]','9|3|11|10');
var subStrings = varkString.split('|');
var visual = varkString[0];
var aural = varkString[1];
var read = varkString[2];
var kinesthetic = varkString[3];
var varkBar = 30*visual
document.writeln('<img src="bar_blue.png" width='+varkBar+' height="25"/>');{
}
Edit: Solved
You are parsing first character when you are getting visual, second on aural and third on read.
I belive that you want to use subStrings
var visual = subStrings[0];
var aural = subStrings[1];
var read = subStrings[2];
when you are slpiting the string varkString the array will automatically constructed and assigned to subStrings.so use it like this:
var subStrings = varkString.split('|');
var visual = subStrings[0];
var aural = subStrings[1];
var read = subStrings[2];
var kinesthetic = subStrings[3];

Categories