I'm writing a task in pascal.
Everything is ok, just my result is not right.
I'm summing some numbers
Example: 2.3 + 3.4+ 3.3 = 9
But output shows: 9.000000 + EEE or something like that.
So- how to convert, to be only 9, not this REAL variable.
To actually convert:
var
i: integer;
...
i := round(floatVar);
To output only the integer part:
writeln(floatVar:9:0);
Let's consider this quite simpler equation:
3.5 + 2.5
What do you expect? 6, right? Let's try this code
write(3.5 + 2.5);
Unfortunately, it's a floating-point number, so it would produce a number represented in a scientific way:
6.00000000000E+00
or 6.0000000000 x 100, or 6 x 10o. Whatever, you only care about 6, who need this weird useless long number? So the idea is to cut off the decimal part and output to the console only the integer part, which can be done with this line of code:
write(3.5 + 2.5 : 0 : 0);
Ok, now it outputs a beautiful number as expected
6
Seems like the problem is solved, but you say that:
I'm summing some numbers
Example: 2.3 + 3.4+ 3.3 = 9
Ohh so that the evenly, beautiful integer is just randomly appeared? Here the problem comes, how do you expect this equation would output?
3.6 + 2.5
It should be 6.1, right? Let's try it with the worked line of code:
write(3.6 + 2.5 : 0 : 0);
And the output is...
6
Unexpected, right? So how about rounding to some decimal places, like 1?
write(3.5 + 2.5 : 0 : 1);
write(3.6 + 2.5 : 0 : 1);
Then, 3.5 + 2.5 = 6.0 and 3.6 + 2.5 = 6.1. But 6.0 may look quite long, so how to make it output 6 for 6.0 and 6.1 for 6.1?
Actually, you can't make the program auto-detect if a real variable contains an integer value because the way a real var is stored is completely different from an integer var (how different they are, please contact Google; but you can do it manually by making a function to do the job).
So my solution is, to be easy, making the output rounded to some decimal places, and that's it.
For purpose of showing pretty output on the screen you can use something like this:
Writeln(result:0:2);
Result on screen would be this:
9.00
What this means someone would ask? Well first number 0 means how wide filed is. if you say it's 0 then Pascal writes it at the very left side of screen. If you said writeln(result:5:2) result would be:
9.00
In other words i would print form the right side and leave 5 chars to do so.
Second number 2, in this example means you want that result printed with 2 decimal places. You can place it only if you want to print on screen value that is real, single, double, extended and so on.You can round to any number of decimals, and if you do writeln(result:0:0) you would get ouput:
9
If you are printing integer and want to have some length of field, lets sat 5 you would do: writeln(int:5). If you added :2 to the end you would get compile time error.
This all also works for something like this: writeln(5/3.5+sqrt(3):0:3),
You should know that this does not round variable itself but just formats output. This is also legal:
program test;
var
a:real;
n,m:integer;
begin
readln(a,m,n);
writeln(a:m:n);
end.
What i did here is i asked user if on how many decimals and with what length of field he wants to write entered number a. This can be useful so i'm pointing it out. Thank you for reading. I hope i helped
You can convert to string, get the int part, e convert to int number!
Or Float to Str than Str to Int:
nPage := StrToInt(FloatToStr(Int(nReg / nTPages))) + 1;
Related
Not understanding this:
Number returned from DataReader: 185549633.66000035
We have a requirement to maintain the number of decimal places per a User Choice.
For example: maintain 7 places.
We are using:
FormatNumber(dr.Item("Field"), 7, TriState.false, , TriState.True)
The result is: 185,549,633.6600000.
We would like to maintain the 3 (or 35) at the end.
When subtracting two numbers from the resulting query we are getting a delta but trying to show these two numbers out to 6,7,8 digits is not working thus indicating a false delta to the user.
Any advice would be appreciated.
Based on my testing, you must be working with Double values rather than Decimal. Not surprisingly, the solution to your problem can be found in the documentation.
For a start, you should not be using FormatNumber. We're not in VB6 anymore ToTo. To format a number in VB.NET, call ToString on that number. I tested this:
Dim dbl = 185549633.66000035R
Dim dec = 185549633.66000035D
Dim dblString = dbl.ToString("n7")
Dim decString = dec.ToString("n7")
Console.WriteLine(dblString)
Console.WriteLine(decString)
and I saw the behaviour you describe, i.e. the output was:
185,549,633.6600000
185,549,633.6600004
I read the documentation for the Double.ToString method (note that FormatNumber would be calling ToString internally) and this is what it says:
By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. If you require more precision, specify format with the "G17" format specification, which always returns 17 digits of precision, or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.
I then tested this:
Dim dbl = 185549633.66000035R
Dim dblString16 = dbl.ToString("G16")
Dim dblString17 = dbl.ToString("G17")
Console.WriteLine(dblString16)
Console.WriteLine(dblString17)
and the result was:
185549633.6600004
185549633.66000035
How do I make a biased random number generator (RNG) in VB.NET?
I know I could make it by fiddling with the output of the Randomize()/Rnd methods, but is there a built-in way of doing this?
I want the biased RNG to give me either a 2 or 4 (though using 1 or 2 as a substitute is also OK by me), with 2 occurring on average 90% of the time and 4 occurring on average 10% of the time.
Create a random number generator to return values from 1-10, if the value from the random number generator is between 1 and 9 send a 2 if the value is 10 send a 4.
You might want to look at this
http://msdn.microsoft.com/en-us/library/vstudio/ctssatww(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
If you want to come out with a mask to generate your values
Here is what I think you can do.
Dim numbers() as integer = {2,2,2,2,4,2,2,2,2,2} ' set 10% for 4, 90% for 2
Dim r as new Random()
Return numbers(r.Next(0, 10))
I'm trying to create a binary to decimal converter, and have got stuck on the code. I have researched forums for any help, but they all seam to use functions, which can not be used within a private sub. Please can anyone give me help on a solution to this problem?
I would use the positional notation method:
http://en.wikipedia.org/wiki/Positional_notation
http://www.wikihow.com/Convert-from-Binary-to-Decimal
So basically, without giving you the answer, you want to loop through binary place holders, filling up a variable as you go along. You would use an index to move from the least significant placeholder to the most.
For example : 10011011 in binary is 155 decimal.
So every place holder is a power with a base of two. Then you add the value for each one until your finished, like so:
placeholder 1 is: 2 pow 0 equals 1.
placeholder 2 is: 2 pow 1 equals 2.
placeholder 3 is: 2 pow 2 equals 4.
placeholder 4 is: 2 pow 3 equals 8.
placeholder 5 is: 2 pow 4 equals 16.
placeholder 6 is: 2 pow 5 equals 32.
placeholder 7 is: 2 pow 6 equals 64.
placeholder 8 is: 2 pow 7 equals 128.
Now we only add for the placeholders that have 1s.
128+16+8+2+1 = 155
What you will need:
A loop looping through indexes, and incrementing the exponent value as you go along, only adding the value if the index equals 1 in the binary number.
Hope my explanation makes sense. Good luck.
I'm working on a homework assignment that asks us to create a type of Units class that can keep track of units and perform basic arithmetic on them. The problem description has this bit, which I don't completely understand:
Probably the easiest way to keep track of the units is to give Units a dictionary that maps symbols to integers. If you are dividing by a unit then it has a negative value in the dictionary. You add two Units together by adding the value together for each symbol in the dictionary. When it is zero, throw the symbol away!
For reference, this is also included in the description:
[...] you could write an expression 3 elephants / (1 sec sec) and it would return the right thing.
Could someone shed some light here? How can I use a dictionary to map these types of units? Am I making this way harder than it needs to be?
It sounds like your teacher is giving you a hint about how to wind up with the proper units at the end of the calculation.
When you're parsing the problem, as you encounter items that are obviously units, enter them into a dictionary. The dictionary would consist of a number and a string (the supposed "unit"). Then you'd use a set of rules to increase or decrease the integer count. The resultant integer value would help you to output the units correctly.
A count of 1 indicates it's a unit in the output.
A count of -1 indicates it's inverse is a unit in the output.
A count of 0 indicates that it doesn't appear in the output at all.
Similarly, a count of 2 would indicate that it's square appears as a unit in the output.
To wit:
5 Hippo + 10 Hippo = 15 Hippos
Parsing: Dictionary:
-------- -----------
5 Hippo Hippo:1
+
10 Hippo Hippo:1 (previous operation was addition or subtraction, and already have Hippo in dictionary
But consider this problem:
5 Hippo * 5 sec/Hippo = 25 sec
Parsing: Dictionary:
5 Hippo Hippo:1
*
5 sec Hippo:1, sec:1
/
Hippo Hippp:0, sec:1 (previous operation was division of Hippo, so decrement Hippo count)
Or perhaps:
10 feet / 5 sec = 2 feet/sec
Parsing: Dictionary:
10 feet feet:1
/
5 sec feet:1, sec:-1 (divided by sec, and second is not in dictionary, so second implicitly = 0. 0 + (-1) = -1.
In the example above, feet will be on the top of the bar because it's equal to 1, and sec will be below the bar because it's value is -1. If it's value had been -2, it would have been (feet/(sec*sec) or feet/(sec squared).
I tried to search this problem and unlucky to solve.
I have a generated report using the rdlc, now, I want to sum all the positive numbers not including the negative in rdlc coding way.
Credit
------
3
3
3
3
3
3
2
-3
-2
------
Total: 20
So, the main point is ignore the negative and sum the positive numbers. so far this what I have tried and this not the solution of my problem.
=IIF(Fields!creditUnit.Value > 0, Sum(Fields!creditUnit.Value), 0)
Anybody can help me?
UPDATE:
This was my temporary solution, since my generated report is good for only one page. I create a parameter for Total
var total = creditsList.Where(c => c.HasValue && c.credit > 0).Sum(c => c.credit.Value);
var totalParam = new ReportParameter("total", total);
I hope that one of you guys can help me in what would be the solution in rdlc coding way to sum all the positive numbers.
UPDATE:
I included the vb.net because the way the coding of rldc is VB syntax.
try
=Sum(IIF(Fields!creditUnit.Value > 0, Fields!creditUnit.Value, 0))
I know its to late but this will work for me
if column have only integer then following expression can be use.
=Sum(IIf((Fields!creditUnit.Value>0), CInt(Fields!creditUnit.Value), 0))
But for decimal or integer and decimal (both) following expression can be use.
=Sum(IIf((Fields!creditUnit.Value>0),CDbl(Fields!creditUnit.Value),0.0))