Round Float Value Based On Condiiton [closed] - python-3.x

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I need to round Value based on certain conditions, please check below.
When Value comes in like
21.43 round to - 21
21.65 round to - 22
21.5 round to - 21.5

Use the round function plus an if statement:
myNum = 21.43
if mynum % 1 != 0.5:
myNum = round(myNum)

You can utilize the round function
Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.
For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise the return value has the same type as number
As an example, round(21.43) returns 21.

Related

Float with no more than 3 decimals [duplicate]

This question already has answers here:
How to display a float with two decimal places?
(13 answers)
Closed 10 months ago.
How to print the calculation of float with only 3 more decimal
If I calculated something and the answer of that calculation is like, 3.33333333...
I just want to print it like 3.333 and that's it
With the round function:
round(3.33333333, 3)
You can use format specifiar
num = 3.3333333
print("%.3f"%num)
Output:3.333
a=1.233455543323
print('{0:.3f}'.format(a))
output=1.233
you can use round function
syntax:
round(number, digits)
number: Required. The number to be rounded
digits: Optional. The number of decimals to use when rounding the number. Default is 0
num = 3.33333333
print(round(num, 3))

Dynamically setting of cells' NumberFormat in VBA excel when the uncertainty calculation in consideration [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to dynamically set the NumberFormat, via VBA, to imported text file values, as just below:
Imported values number type is "General", so I want to change to "Number", but preserve the decimal point
12.324 ............................ 3 decimal points
2.12 .............................. 2 decimal points
0.00123 ............................5 decimal points
12.1234567 ........................ 7 decimal points
And all the data in the same column.
Code is simple:
Range("A1").Numberformat ="##0.0##"
Range("A2").Numberformat ="#0.0#"
Range("A3").Numberformat ="#0.0####"
Range("A4").Numberformat ="##0.0######"
So, I need to dynamically set "#".
The reason for setting the number after decimal point is very important for calculating the uncertainty result. In calculations, there is a need for the resolution which means how many digits after the decimal point appears on the measurement device that doing calibrations.
I found a solution.
res_dec_point = Len(Split(res_val & ",", ",")(1))
kes_dec_point = Len(Split(kes_val & ",", ",")(1))
MyNumberFormat_res = "0."
For i_num = 1 To res_dec_point
MyNumberFormat_res = MyNumberFormat_res & "0"
Next i_num
MyNumberFormat_kes = "0."
For i_num = 1 To kes_dec_point
MyNumberFormat_kes = MyNumberFormat_kes & "0"
Next i_num
Thank you https://stackoverflow.com/users/10968406/bdra
In a nutshell, as I understand it, your code needs to read decimal numbers formatted as string values, then write them to destination cells formatted as decimal numbers. Each destination cell needs to be formatted with as many decimal places as are present in the original string value.
You did not include your code in the question, so I will not provide completed code in response, but in outline here is a way to approach it:
Identify the number of decimal places in the original string value. Use the Instr function to locate the decimal point, then count the number of numerals to the right of the decimal point.
Number formats themselves are expressed as string values. For instance, in Range("A1").Numberformat ="0.0", the "0.0" part is a string. You can utilize this to your advantage. Either look up or dynamically generate a string value that expresses the required number format for each cell.
Assign the number format to the destination cell.
Here is a snippet that generates and assigns a number format with a variable number of decimal places:
Dim MyNumberFormat As String, NumberOfDecimals As Integer
MyNumberFormat = "0."
For i = 1 To NumberOfDecimals
MyNumberFormat = MyNumberFormat & "0"
Next
MyWorksheet.Cells(1, 1).NumberFormat = MyNumberFormat
I believe this answers the core of your question. You will need to build it out with your own code, define what happens if NumberOfDecimals is 0, etc.
The formatting of a value does not change the value
In this example we calculate 2/3, and show it using 'General format', and using 3,4, or 5 decimals.
On the last line we substract 0.666 from the values in the first line. The green values are all in 'General format'.
Conclusion: The formatting does not change the value!

What is the function of round() with this strange behavior? [duplicate]

This question already has answers here:
Python 3.x rounding behavior
(13 answers)
Closed 3 years ago.
I want to round some int numbers but I came across with the strange feature of round() for example
round(2.1) = 2
round(2.5) = 2 #it rounds to ceil
round(2.7) = 3
it rounds differently with the odd number as follow
round(5.1) = 5
round(5.5) = 6 #it rounds to floor
round(5.7) = 6
it rounds the X.5 to the floor with the x = even numbers but with the X = odd numbers it rounds to the ceil
I want to ask what is the advantage of this round? and where can I use it in our examples ? or what is its usage?
Looks like if it's close it goes to the even option.
From the documentation https://docs.python.org/3/library/functions.html#round
Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.
For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise the return value has the same type as number.
For a general Python object number, round delegates to number.round.
Note The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

Calculations being being rounded SQL Server 2012 [duplicate]

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Closed 7 years ago.
I am trying to calculate some performance metrics as [RATE] in SQL but no matter what I do I am only getting integer values in return.
In my dataset I have a Numerator and a Denominator both stored as integers that are broken down into groups and subsets. The rate is calculated slightly differently depending on the group and subset. For group 1 the calculation is simply N/D. For group 2 the calculation is (N*1000)/D with the exception of 1 subset which is calculated (N*10000)/D.
I wrote the query as:
SELECT [Group]
,[SubSet]
,[Numerator] N
,[Denominator] D
,CASE WHEN D=0 Then NULL
WHEN [Group]='G1' THEN [N]/[D]
WHEN [SubSet]='S2' THEN ([N]*10000)/[D]
WHEN [SubSet] NOT LIKE 'S2%' AND [G] NOT LIKE 'G1%' THEN ([N]*1000)/[D] as [RATE]
No matter what I do the outcome variables are integers. I tried formatting RATE as varchar, decimal, and float with no success. I tried changing N and D's format to varchar, decimal, and float as well. I tried changing the equations from (N*1000)/D to (N/D)*1000 but still no effect.
What am I missing/doing wrong?
The problem you are having is because SQL is doing integer division, which will only return whole numbers. To get a decimal return value you must have at least one value as a decimal.
Try this:
(CAST([N] as decimal(12,6))/[D]) * 1000
Adjust decimal(12,6) based on the precision you are expecting. 12,6 will return a decimal with 6 digits after the decimal point. If you wanted only 2 decimal places use 16,2.
If you then want to round the calculated value you will need to make use of the ROUND function in SQL.
Round to the second decimal place:
ROUND((CAST([N] as decimal(12,6))/[D]) * 1000, 2)
You need to use CAST:
CAST ((N*1000) AS FLOAT) / D
Hope this helps.
SELECT (n * 1000.0) will do it.

RE: Odd length string over { 0, 1} that contains exactly two 0's [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I just started learning about formal lang and automata theory, and recently learned about regex, so I don't know any complicated symbols, so please stick with basic symbols.
The question is: Write a regex for the following language over {0, 1} that is a set of all odd length strings that contain exactly two 0s.
I've got the first part finished (the odd part), it should be:
(0+1)[(0+1)(0+1)]* ( + is the same as | (or) I believe, we learnt it as +)
However, when I think about having exactly two 0s it gets really messed up. I can only see that I can use * with 1 only since # of 0s are limited to 2.
But if I do (11)* , I can't get the permutation of 0s inside the 1s. (e.g. can't get 10101 with (11)*).
What I know:
Only 1s can use *
In the regex only two 0s will be used
The way to make odd length is to add an odd length to an even length (even length needs to have empty string within it's set)
Odd length should not use * since 2 odd = even, so only even length can use *.
For possible hints or answer, please use 0, 1, +/|, *, (, ) only. Some other expressions I will not be able to understand.
Regular Language over {0, 1} that is a set of all odd length strings that contain exactly two 0.
What this language means?
Note language string can be consist of two 0 and any number of 1 such that total length of string is odd. There is no other restriction. 1 and 0 an appear in any order and in any pattern.
As we know even + odd = odd. So in string is consist of at least three length and odd number of 1 because number of 0 in string is two.
So regular expression should be something like: A 0 B 0 C , where A, B, C, are substrings consist of only 1 and total number of 1 in A, B, C is odd, hence all can't be ^ (nul) in a expression.
Now because total number of 1 in A, B, C = odd, so it can be something like: either(1) two even and one odd or (2) all three are odd.
Note: a odd length string can't be null.
Regular Expression:
1(11)*01(11)*01(11)* + 1(11)*0(11)*0(11)* + (11)*01(11)*0(11)* + (11)*0(11)*01(11)*
// all odd A odd, B C even B odd, A C even A B even, C odd

Resources