How can I always round down to the closest 10 whole number? - coldfusion

So, I have a query that returns a number of records. And on my results page I display a line that says: Results 1 to 10 out of 30+ results are shown below.
The 30+ number I get by doing this:
<!--- round to the nearest ten --->
<cfset totalfoundRounded = Round(myquery.recordcount/ 10) * 10>
This works great if my recordcount is for example 34 or less. Or 24 or less, basically as soon as it gets over 5, my code rounds up. So even if there are only 18 records, it will say 20+ records found.
I have been scratching my head on how to get this logic to work, so that it always rounds down to the nearest 10, even if it's 18, or 19.
I tried using int() but that didn't work, it only works on decimal places.
So, is there a function in coldfusion, or some technique I'm missing to get this to work? I have been unable to find anything searching, and math is not my strong point :(
Thank you very much for any suggestions!!

Try this?
<cfset totalfoundRounded = Int(myquery.recordcount/ 10) * 10>

What do you want it to say when it's less than 10? 0+? You can probably do number - (number % 10). This will always round down to the nearest multiple of 10.

What about this?
function Roundupdollars(amt) {
roundedAmt = amt * 100
roundedAmt = Round(roundedAmt)
roundedAmt = roundedAmt / 100
return roundedAmt;
}

Related

Primes in arithmetic progressions in Sagemath

I am in need of finding prime numbers in arithmetic progression
80218110*n+8021749, n=1 to 100,000
I was told that using Sage would be a good option, since my computer is old. I happen to be new to Sage and I haven't found it to solve my problem, I guess it shouldn't be difficult, does anyone have a good reference for printing primes in arithmetic progressions?
SageMath is based on Python, and Python provides a syntax which should be comfortable for mathematicians:
[80218110*n + 8021749 for n in range(100)]
range(100) is the ordered set 0, 1, 2, ..., 99, and so the previous line evaluates 80218110*n + 8021749 for these values of n. We can also test whether the entries are prime:
INPUT: [80218110*n+8021749 for n in range(100) if (80218110*n+8021749).is_prime()]
OUTPUT:
[8021749,
489330409,
569548519,
970639069,
1050857179,
1131075289,
1772820169,
2093692609,
2173910719,
3136528039,
3617836699,
4660672129,
4740890239,
5382635119,
6425470549,
7067215429,
7227651649,
7548524089]
You can of course make the argument to range larger, but maybe it's not a good idea to print the whole list.
INPUT: len([80218110*n+8021749 for n in range(100000) if (80218110*n+8021749).is_prime()])
OUTPUT: 15273
(len returns the length of the list.) Producing this list is pretty quick, at least on my computer:
INPUT: %time L = [80218110*n+8021749 for n in range(100000) if (80218110*n+8021749).is_prime()]
OUTPUT CPU times: user 94.6 ms, sys: 1.13 ms, total: 95.8 ms
Wall time: 95.5 ms
(ms is milliseconds.)

How to conditionally display decimals in a measure in Power BI

Let's say I have a simple measure like this:
SimpleMeasure = MyTable[Column1]/MyTable[Column2]
Is it possible to have the number of displayed decimals dependent on the value? For example, if my measure calculates the number 500, then I don't want to see 500.00 but rather 500. However if my measure calculates 0.56, then I want to see the value displayed like that to two decimal places and not rounded to the number 1.
So a possible visualization table would look like this:
Store SimpleMeasure
00 10
01 18
02 0.67
03 6
Thank you in advance!
IF(
[SimpleMeasure] < 1, FORMAT([SimpleMeasure], "#,##0.##",
FORMAT([SimpleMeasure], "#,##0")
)
You can use the Switch() function. It will return the first True result. Put you values in a DESC order - start with the largest value, then come to a lower and then to the lowerest. For example :
Switch(
True
,[SimpleMeasure]<1,Round([SimpleMeasure],2) -- 0.655->0.66
,[SimpleMeasure]>999,Round([SimpleMeasure],-2) --1044->1000
,[SimpleMeasure]>499,Round([SimpleMeasure],-1) -- 505,5->510
,[SimpleMeasure]>99,Round([SimpleMeasure],0) -- 101,55->102
,[SimpleMeasure]>1,Round([SimpleMeasure],1) -- 99,43->99.4
)
Or/and you can use in the Switch() the Format() function, with or instead of Round() like:
Format([SimpleMeasure],"#,##0.#")
Or
Format(Round([SimpleMeasure],1),"#,##0.#")
-- Both of them Format() will return and show 99,43 as 99,4.
-- Placeholer # returns a digit or nothing.
-- Placegholder 0 returns a digit or 0.
Usage of Format() and Round() depends on your task. But for a selective Fomat you will need the Switch().
Hope, the answer will help you solve the case

Adding values from multiple .rrd file

Problem =====>
Basically there are three .rrd which are generated for three departments.
From that we fetch three values (MIN, MAX, CURRENT) and print ins 3x3 format. There is a python script which does that.
eg -
Dept1: Min=10 Max=20 Cur=15
Dept2: Min=0 Max=10 Cur=5
Dept3: Min=10 Max=30 Cur=25
Now I want to add the values together (Min, Max, Cur) and print in one line.
eg -
Dept: Min=20 Max=60 Cur=45
Issue I am facing =====>
No matter what CDEF i write, I am breaking the graph. :(
This is the part I hate as i do not get any error message.
As far as I understand(please correct me if i am wrong) I definitely cannot store the value anywhere in my program as a graph is returned.
What would be a proper way to add the values in this condition.
Please let me know if my describing the problem is lacking more detail.
You can do this with a VDEF over a CDEF'd sum.
DEF:a=dept1.rrd:ds0:AVERAGE
DEF:b=dept2.rrd:ds0:AVERAGE
DEF:maxa=dept1.rrd:ds0:MAXIMUM
DEF:maxb=dept2.rrd:ds0:MAXIMUM
CDEF:maxall=maxa,maxb,+
CDEF:all=a,b,+
VDEF:maxalltime=maxall,MAXIMUM
VDEF:alltimeavg=all,AVERAGE
PRINT:maxalltime:Max=%f
PRINT:alltimeavg:Avg=%f
LINE:all#ff0000:AllDepartments
However, you should note that, apart form at the highest granularity, the Min and Max totals will be wrong! This is because max(a+b) != max(a) + max(b). If you dont calculate the min/max aggregate at time of storage, the granularity will be gone at time of display.
For example, if a = (1, 2, 3) and b = (3, 2, 1), then max(a) + max(b) = 6; however the maximum at any point in time is in fact 4. The same issue applies to using min(a) + min(b).

division gives 0 in loop, why?

the code below gives me 0% for each 20th iteration, instead of the actual percentage I would like it to show.
n=100
for i in range(n):
if i% 20 ==0:
print str(i/n*100) + '%, Progress'
Results:
0%, Progress
0%, Progress
etc.....
I must be missing something really simple. Thank you.
change the division to i/(float)n*100 so that the resulting output will be formatted to decimal points by the python interpreter.
Division automatically rounds down to the nearest integer.
What happens in your code is:
i = 20
n = 100
i/n = 20/100, which becomes 0.
Then (i/n)*100 = 0*100 = 0.
You could solve this by first multiplying i by 100 and then dividing by n:
i*100/n
n=100
for i in range(n):
print(i);
if i% 20 ==0:
print str((float(i)/float(n))*100) + '%, Progress'
for python i/n is an (int)/(int) according to your variable declaration. so ti tries to give an int answer for i/n which is always 0.
In each iteration i/n*100 gets rounded down to the nearest integer (this is how the division operator works on integers in python2).
You could either use explicit casting to float() or execute from __future__ import division beforehand. This would prevent the division operator from rounding down automatically.
Here you can find a detailed description of a similar problem.

c++ random numbers -100 to 100

I want to generate a list that contains random numbers from -100 to 100. The code I have so far just goes from -50 to 50.
for(int line=0;line<100;line++) //reads individual lines and compares them
{
ofs<<rand()%101 + (-50)<<endl;
}
I would really appreciate it if someone could point me in the right direction!
Change to %201 and +(-100). A little experimentation would have figured this out.
Note that you want values from -100 to +100, which is a range of 200 values, so you'd really want:
rand() % 200 // possible off-by-one error
This will give you 0..199. To get closer to the range you want, subtract 99:
(rand() % 200) - 99
This will give you the range -99..+100. This is closer to what you want.
You should be able to go back now and tweak to get the range you really want.
rand() %201 - 100 works just right