I am using mvc3 with c# language where I was stuck in calculation problem.Here I am using double type variables for 3 properties Qty,Cost and totalprice
Here totalprice=Qty*Cost;
I have requirement where I want to get totalprice of product without decimals. For example if qty=14.3 , cost=15. Then on java script/C# I will get total price 214.5
But I require 214.To solve this I used Math.Floor(214.5) to get 214. But when Qty=18.9 and cost=1500. Then on javascript or C# multiplication, I am getting total price=28349.999999999996, The correct result should be 28350. Please help me to get solution where I will get both result accurately
Choose according to you in c#.
-3 -2 -1 0 1 2 3
+--|------+---------+----|----+--|------+----|----+-------|-+
a b c d e
a=-2.7 b=-0.5 c=0.3 d=1.5 e=2.8
====== ====== ===== ===== =====
Floor -3 -1 0 1 2
Ceiling -2 0 1 2 3
Truncate -2 0 0 1 2
Round (ToEven) -3 0 0 2 3
Round (AwayFromZero) -3 -1 0 2 3
In javscript do like this:
var qty=qty.toFixed(1);
var cost=cost.toFixed(1);
var totalprice=Math.round(qty*cost);
you can use Math.round(); this solves your problem.
Math.round(214.5- .1);
This question already has an answer here: Javascript floating calculation error
you can use toFixed() for example: (1.2 - 1).toFixed(1) * 1 // 0.2
Related
I'm currently learning Java and JavaScript (alongside HTML and CSS).
I've been struggling with logical and arithmetic operations for a long time as I've never been good at maths in school. My question is : What is the best, more straight-forward (aka with the less steps possible) way to calculate a modulus operation ?
For exemple, here is how a find the result for 38 % 5 :
This is my step-by-step method to find the result.
38 % 5
First, I'll divide 38 by 5 :
38 / 5 = 7.6
Here, I'll make sure the result is correct :
7.6 * 5 = 38
I've noticed modulus results oftenly (if not always) have decimals (7.6; 4.333; ...).
Somewhat somehow, I had the idea to make 7 * 5 :
7 * 5 = 35
Good ! So, if I make a last substraction, I should get the modulus, right ? :
38 - 35 = 3
Let's check it, and it's correct ! :
38 % 5 = 3
Hence, I found the result of 38 % 5.
Now, I'm not particularly sure that this is the proper way to calculate a modulus by yourself (aka by using step-by-step operations on a calculator rather than using a Java / JavaScript operation) but that's how I do and it works. I could stop the thinking now and roll with it but I want to be sure that I'm doing the right thing before taking a bad habit.
My question is simple : Does my method will systematically work or not ?
If not, what should I do ? How should I calculate this simple operation ?
Thanks.
The way you're doing it is fine if you have to do it by hand for some reason. The modulus operator is just finding the remainder of division with numbers A and B. So, as you said, if you have a decimal, remove it and multiply those two numbers together, then subtract the result from the original dividend and the result will be the modulus of the two numbers.
A few examples
9 % 3 = 0 because 3 goes into 9 equally 3 times leaving us with no remainder.
12 % 5 = 2, 5 goes into 12 twice with a remainder of 2 or using your method:
12/5=2.4 -> 2*5=10 -> 12-10=2 so 12%5=2.
I have a simple byte flag with two bits that represents if a person was invited to, and is attending, an online interview. Attendance is represented by two Radio selectors in a form, and could be either Yes, No, or NULL. Invited can be NULL.
bit index 0 = INVITED?
bit index 1 = ATTENDING?
7 6 5 4 3 2 1 0 bit index
=========================================================
128 64 32 16 8 4 2 1 binary notation
=========================================================
1 1 invited, attending
1 0 invited, not attending
0 1 not invited, attending
0 0 not invited, not attending
The user can change the Attending bit via radio controls in the
form. It can be Yes, No, or NULL.
The user cannot switch the Invited bit. It is set to 1 on initial
import, otherwise, it is NULL.
The Invited bit is set to 1 upon initial import into the
application. However, the Attending bit should be NULL, since there
we don't know yet if they're attending.
That's my problem!! The Attending bit is naturally 0, because the user hasn't confirmed if you are attending.
I don't know how to represent the state of "on initial import, Attendance is unconfirmed"
Can I represent a "NULL" state at bit index 1? Should I just add an additional bit to represent an unconfirmed state? Should I change my byte flag to better represent what's going on?
Something like this. I would have to remember to clear bit index 2.
bit index 0 = INVITED?
bit index 1 = ATTENDING?
bit index 2 = UNCONFIMRED ATTENDANCE?
7 6 5 4 3 2 1 0 bit index
=========================================================
128 64 32 16 8 4 2 1 binary notation
=========================================================
1 0 1 invited, not attending, attendance not established yet
0 1 1 invited, attending, attendance established
I am looking for regex expression which takes -10 to 10 with 2 decimal numbers upto 12. means cases passed are -10.01 to -10.11 for 10 years and 11 months.
I was able to make a regex for -10 to 10 but adding decimal till .11 was not able to figure out If somebody can help that would be great.
cases passed -
0
1
10.01
10.00
10.11
-10.00
-10.01
10.12
-10.00
10
cases should fail -
10.12
10.13
-10.13
11
-11
-10.13
I presume that values such as 9.08 and -5.04 are also valid? In that case this regex: ^-?(10|[0-9])(\.(0[0-9]|1[01]))?$ will match the values you want. The first part looks for a number from -10 to 10; the second part looks for an optional decimal part which can be something from 00-09 or 10 or 11.
Regex101 demo
This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(39 answers)
Closed 5 years ago.
I am trying to generate random 0 or 1 as I am writing a script to populdate my database. If it is 1, I will save it as male and 0 the other way around.
Inside my JavaScript:
Math.floor((Math.random() * 1) + 1);
I used this to generate either 1 or 0. However, with the code above, it always return me with 1. Any ideas?
You can use Math.round(Math.random()). If Math.random() generates a number less than 0.5 the result will be 0 otherwise it should be 1.
There is a +1 with Math.random, so it will always going to add 1 to the randomly generated number.
You can just randomly generate a number, since Math.random will generate any floating number between 0 & 1, then use if.. else to assign 0 or 1
var y = Math.random();
if (y < 0.5)
y = 0
else
y= 1
console.log(y)
I'm trying to build a Google Line Chart with MySQL data and while I understand Javascript and how to get data with PHP I'm having a hard time wrapping my head around getting and/or formatting the data in the correct manner.
Right now my table looks like this
PERSON WEEK PRODUCTION
Bob 2 1
Bob 3 0
Bob 4 0
Bob 5 2
Bob 6 0
Paul 1 0
Paul 2 0
Paul 3 0
Paul 4 2
Paul 5 3
Paul 6 1
Mike 1 0
Mike 2 1
Mike 3 1
Mike 4 4
Mike 5 0
Mike 6 1
Ron 1 1
Ron 3 0
Ron 4 0
Ron 6 0
And the graph I essentially want create looks like this
I was able to make that in the Google Code Playground by manually tweaking the data to look like this
WEEK BOB PAUL MIKE RON
1 0 0 0 1
2 1 0 1 0
3 0 0 1 0
4 0 2 4 0
5 5 3 0 0
6 0 1 1 0
So my big question though is how do I do this in an automated fashion? Obviously I need to either modify my sql query to do the heavy lifting there or massage the data with PHP afterwards, but I'm as lost as a babe in the woods on how to do that. Further complicating the issue, at least in my eyes, is that in the original data set not every has all the weeks, but needs them to be created and zero'ed out in the final result set. Also while I'm showing 4 persons it could be 1 to N.
If anyone could point me in the right direction I would be eternally grateful!
UPDATE
#asgallant pointed me to this example he had written up - http://jsfiddle.net/asgallant/HkjDe/
I tried implementing the solution he presented, which can be seen here - http://thejspot.ws/qa/table.php
While the first table populates, the second table never displays. I've tracked it down to this line of code, but I'm not sure what the issue is.
var pivotedData = google.visualization.data.group(view, [0], groupColumns);
Any suggestions?