I am sorry if it's already answered in here, but I couldn't find the exact solution I am looking for.
I am trying to output the result of a division of two variable which is continuously changing. The result may vary from 0.00 to 100.12314235234523 (not exactly this specific, I just wanted to give you an idea). I want to print the result only 2 digits after the decimal point, if there is nothing after decimal, it should print 2 zeroes after decimal.
For example:
10 / 5 = 2.00
23 / 6 = 3.83
I don't need to round up the result for example, if the output is: 73.4869999, I don't need it to be 73.49, 73.48 is fine with me.
What I have written so far is:
packet_loss[2]->setText(QString("%1%2%3").arg(((data.packet_loss_tx) * 100) / data.packets_tx).
arg(locale.decimalPoint()).
arg((((data.packet_loss_tx) * 100) % data.packets_tx), 2, 10, QChar('0')));
But this prints all the values after the decimal point. I can divide this part arg((((data.packet_loss_tx) * 100) % data.packets_tx) with 10, 100 or 1000 to reduce the number of decimals after the decimal point but this is a variable which changes every seconds. So if the output is 3 digits after decimal and I divide it by 10, I will get proper output, but the next value may be 5 digits after decimal and division by 10 will give me 4 digits after decimal. I want the final output to show only 2 digits after decimal.
You could try to use QString::number() function with specific formatting options (two digits precision). For example:
auto s = QString::number(100.12914235234523, 'f', 2); // Gives "100.13"
Besides, if you use floating point numbers, it's better to multiply them with floating point numbers too. I.e. you need to perform your calculations using 100.0 instead of integer value 100.
Related
Numbers under 1 are currently being represented with a leading zero before the decimal point (example: 0.50). Because I'm working with baseball statistics (which almost never have the zero before the decimal) I would like to remove that. I want to keep the number before the decimal if its greater than 1 though. How would I do that?
For instance if I'm working with this measure. Is there something I can add to that?
AVG = SUM(Batter[H])/sum(Batter[AB])
Thanks. I appreciate the help.
Here is some sample data
Name AB H
Gleyber Torres 546 152
Brett Gardner 491 123
Aaron Judge 378 103
Adam Ottavino 0 0
Aroldis Chapman 0 0
The NAN error is occurring because you are dividing by 0. You should add an IF condition to avoid that:
AVG = IF(sum(Batter[AB])=0,BLANK(),SUM(Batter[H])/sum(Batter[AB]))
To tackle the formatting issue you can use the FORMAT function as mentioned by Andrey:
AVG = IF(sum(Batter[AB])=0,BLANK(),FORMAT(SUM(Batter[H])/sum(Batter[AB]),"###.0#"))
Hope this helps.
Unfortunately, it isn't directly possible. However, in the last step (the visualization of the data), you can convert the decimal number to text and format it as you want. For example, your measure could be like this:
AVG = FORMAT(SUM(Batter[H])/SUM(Batter[AB]), "#,###.00")
This will give you 2 decimal places (0 means that there will be a digit displayed at this position), but the digits before the decimal are optional (# means it will show a digit, but will omit the leading zeros) or here are some examples:
I want to add 1 to the very end of a float, like this:
0.0735 + 0.0001 = 0.0736
But I'll get different lengths of floats, and for each I want to add 1 to the very end, like this:
0.000398 would have to be added to 0.000399 and 0.000000281 would have to be 0.000000282.
Also, if it's something like this: 0.0000280, I need to add it to the 0, not the 8. Which makes me believe that I have basically one problem: I need to find the first number after 0 and then count 2 more whatever it is, and add 1 to it (basically keeping 3 numbers, even if there's a 0 after)
Is there a way to do that?
edit: I tried this already len(str(n)) but the problem is the zeros, as I don't have control over the length of the number, a number like this: 0.01 should be added to 0.0101 but if I have 0.0111 I want 0.0112. What I mean is, the program usually hides the zeroes, that's why I said I'd probably need to find the first non zero digit on it.
10^floor(log10(n)) will get you a 1 digit at the same position as the first digit (e.g. 0.0001 for 0.000399), so add 0.01 times that:
n = 0.0735
n += 0.01 * 10 ** math.floor(math.log10(n))
I have a regexp to check for a decimal with 2 numbers, but I want to check both the integer and the decimal part for their length.
/^\s*-?[1-9]\d*(\.\d{1,2})?\s*$/;
The above code is decimal with length 2 (ex: 12.23) but I want 10 integer value and 2 decimal value (10,2) like,
1234567890.12
Use /^(?![.])\d{0,10}(\.\d{1,2})?$/
It allows 1.23, 1.2 0.2
Invalid values ., 1.
Depending on what you exactly want, you can use:
/^\s*-?(\d{1,10}(\.\d{1,2})?)\s*$/
for input like: 12.23, 3.4, 1234567890.34, 4, 456, etc., or:
/^\s*-?(\d{10}(\.\d{1,2})?)\s*$/
for: 9087654321, 1234567890.1, 1234567890.23 (10 digits, and optional point and one or two digits), or:
/^\s*-?(\d{10}\.\d{2})\s*$/
for exactly 10 digits fallowed by point and 2 digits, like: 9087654321.12, etc. Its all depends on what kind of numbers you want to filter.
I am doing some financial trading work. I have a set of stock symbols but they have very clear pattern:
it's composed of two characters AB, AC AD and current month which is a four digit number: 1503, 1504, 1505. Some examples are:
AB1504
AB1505
AC1504
AC1505
AD1504
AD1505
....
Since these strings are so well designed patterned, I want to map (hash) each of the string into a unique integer so that I can use the integer as the array index for fast accessing, since I have a lot of retrievals inside my system and std::unordered_map or any other hash map are not fast enough. I have tests showing that general hash map are hundred-nanoseconds latency level while array indexing is always under 100 nanos.
my ideal case would be, for example, AB1504 maps to integer 1, AB1505
maps to 2...., then I can create an array inside to access the information related to these symbols much faster.
I'm trying to figure out some hash algorithms or other methods that can achieve my goal but couldn't find out.
Do you guys have any suggestions on this problem?
You can regard the string as a variable-base number representation, and convert that to an integer. For example:
AC1504:
A (range: A-Z)
C (range: A-Z)
15 (range: 0-99)
04 (range: 1-12)
Extract the parts; then a hash function could be
int part1, part2, part3, part4;
...
part1 -= 'A';
part2 -= 'A';
part4 -= 1;
return (((part1 * 26 + part2) * 100 + part3) * 12 + part4;
The following values should be representable by a 32-bit integer:
XYnnnn => (26 * X + Y) * 10000 + nnnn
Here X and Y take values in the range [0, 26), and n takes values in the range [0, 10).
You have a total of 6,760,000 representable values, so if you only want to associate a small amount of data with it (e.g. a count or a pointer), you can just make a flat array, where each symbol occupies one array entry.
If you parse the string as a mixed base number, first 2 base-26 digits and then 4 base-10 digits you will quickly get a unique index for each string. The only issue is that if you might get a sparsely populated array.
You can always reorder the digits when calculating the index to minimize the issue mentioned above.
As the numbers are actually months I would calculate the number of months from the first entry and multiply that with the 2 digit base-26 number from the prefix.
Hope you can make some sense from this, typing on my tablet at the moment. :D
I assume the format is 'AAyymm', where A is an uppercase character yy a two digit year and mm the two digit month.
Hence you can map it to 10 (AA) + Y (yy) + 4 (mm) bits. where Y = 32 - 10 - 4 = 18 bits for a 32 bit representation (or 262144 years).
Having that, you can represent the format as an integer by shifting the characters to there place and shifting the year and month pairs to there places after converting these to an integer.
Note: There will always be gaps in the binary representation, Here the 5+5 bit representation for the characters (6 + 6 values) and in the 4 bit month representation (4 values)
To avoid the gaps change the representation to ABmmmm, were the pair AB is represented by a the number 26*A+B and mmmm is the month relative to some zero month in some year (which covers 2^32/1024/12 = 349525 years - having 32 bits).
However, you might consider a split of stock symbols and time. Combining two values in one field is usually troublesome (It might be a good storage format, but no good 'program data format').
I am trying to find a regular expression that works for a decimal with a max of 2 digits before the decimal point and 2 digits after the decimal point. The decimal point and decimal places are optional. So these values would be accepted :
90
5.4
45.21
0.5
0
And the would be rejected :
100
105.56
05.6
55.543
78.
Can any regex gurus help?
This should work:
^[1-9]\d?(?:\.\d{1,2})?$
If you want to accept even 0.5, try this:
^(?:[1-9]\d?|0)(?:\.\d{1,2})?$
I hope this will work for you
\d{0,2}(\.\d{1,2})?
/\A[1-9]?\d(?:\.\d{1,2})?\z/
..................