Express decimal with precision of 2 in KQL? - precision

I have a value, expressed in bytes, being returned from an Azure Log Analytics query:
I want to convert this to megabytes and make it more human readable. In this case, "4.19 MB".
When I try to convert the byte value into megabyte, I can't seem to get KQL to add the desired precision of 2 places.
Tried:
RequestBodySize = strcat(round(RequestBodySize / 1000 / 1000, 2), ' MB') but this results in "4.0 MB".
How do I get this value to correctly reflect a precision of 2?
EDIT 1:
format_bytes(RequestBodySize, 2) returns "4 MB". No precision.
Same with `format_bytes(RequestBodySize, 2, 'MB')

I used a simple query to simulate the case and it works as expected for me.
In the first example, I added the unit to the field's name to maintain consistent value format that is aligned with the way values are projected in queries:
AzureDiagnostics
| where TimeGenerated > startofday(ago(20d))
| summarize volumeSizeMB = round(sum(_BilledSize)/pow(1024,2),2)
Results:
17.27
And when adding the unit to the value:
AzureDiagnostics
| where TimeGenerated > startofday(ago(20d))
| summarize volumeSize = strcat(round(sum(_BilledSize)/pow(1024,2),2), ' MB')
Results:
17.27 MB
If your issue persists and you don't see the expected precision, I suggest you open support case to have it investigated.

Use print format_bytes(12345678) to get 12 MB.
Use print format_bytes(12345678, 2) to get 11.77 MB.
Read the doc for more info.

Hi The answers above me are great,
just wanted to add one small input.
the reason you are not getting the fraction after the decimal is because you are dividing two integers.
to get a real number you will need to first convert one of the numbers to float or double, by using the todouble() toflout() https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/todoublefunction
RequestBodySize = strcat(round(todouble(RequestBodySize) / 1024 / 1024, 2), ' MB')
or, as suggested by Yossi, just multiply by 1.0
RequestBodySize = strcat(round(1.0 * RequestBodySize / 1024 / 1024, 2), ' MB')

try something like this:
| summarize GB = 1.0 * sum(TheThingsYouSub) / 1024 / 1024 / 1024 by SomeFilter

Related

Division on rails 4

I'm having a hard time on thinking what should I do with this problem. I'm dividing the two numbers and expecting a non-whole number answer. Meaning to say it should be on decimal format. But unfortunately it answers a whole number.
example: 5 / 2 = 2
s.apts = sum_pts.to_f / sum_game.to_f
You just need to tell Ruby you are doing non-integer division, by writing the problem as "5.0 / 2.0"
See:
Why is division in Ruby returning an integer instead of decimal value?
Try this:
a = 5.0
b = 2.0
puts a / b

Logic in Custom Rounding off - VB Script - QTP

I have a data file where decimal points aren't specified for a decimal number. The number is just described in the layout for the data file as first 2 digits as real and next 2 digits as decimal and it varies for different fields, the real and decimal part
So an actual number 12345.6789 is specified as 123456789. When I want this to be rounded off to 2 decimal points to match the value in application, I use the below logic
Public Function Rounding(NumberValue, DecimalPoints, RoundOff)
Rounder= Roundoff+1
Difference = DecimalPoints - Rounder
NumberValue = Mid(NumberValue, 1, Len(NumberValue)-Difference)
RealNumber=Mid(NumberValue,1,Len(NumberValue)-Rounder)
DecimalNumber=Right(NumberValue,Rounder)
NumberValue = RealNumber&"."&DecimalNumber
NumberValue = Cdbl(NumberValue)
NumberValue = Round(NumberValue, Roundoff)
Rounding = FormatNumber(NumberValue,Difference+1,,,0)
End Function
However the problem with this logic is that I am not able to round off decimals when the number has 0 as the decimal value
For an Example, lets take 12345.0000 which I want to round off to 2 decimal points
My function returns it as 12345 whereas I want this to be returned as 12345.00
Any ideas on how this logic could be tweaked to get the desired output or is that not possible at all?
To get the decimal places, use the Formatnumber function. See http://msdn.microsoft.com/en-us/library/ws343esk(v=vs.84).aspx - the default is normally 2 decimal places, but it is region settings specific when using the defaults.
Your script also has a small issue if the decimalpoints variable matches the roundoff variable - it will not populate Rounding with a result. I am also not sure why you are comparing DecimalPoints to Roundoff (-1) ?
I've revised the entire routine - it should do what you want (although I don't know what values you are feeding it) - So now it will work like this:
Doing 4 digits:
Rounding (123450001, 4, 2)
Result:
12345.00
Doing 2 digits:
Rounding (123450001, 2, 2)
Result:
1234500.01
Doing 4 digits (increments if > .5)
Rounding (876512345678, 8, 4)
Result:
8765.1235
Revised simplified function that should do everything you are asking:
Public Function Rounding(NumberValue, DecimalPoints, RoundOff )
RealNumber = Mid(NumberValue, 1, Len(NumberValue)-DecimalPoints)
DecimalNumber = Round("." & Right(NumberValue,DecimalPoints), RoundOff)
Rounding = FormatNumber(RealNumber + DecimalNumber,RoundOff,,,0)
End Function
Here's a working version of your Function:
Public Function Rounding(NumberValue, DecimalPoints, RoundOff)
RealNumber=left(NumberValue,Len(NumberValue)-DecimalPoints)
DecimalNumber="." & Right(NumberValue,DecimalPoints)
NumberValue = RealNumber + DecimalNumber
NumberValue = Round(NumberValue,RoundOff)
Rounding = FormatNumber(NumberValue, RoundOff,,,0)
End Function
I'm pretty sure you won't be able to use the Round() function for what you need. Take a look at the FormatNumber() or FormatCurrency() functions as they have the option to "IncludeLeadingZero".
Take a look at the answer from the following link for more information:
vbscript round to 2 decimal places using Ccur

Why does the result being so different

I was new to Matlab,and this time I want to create a function for its image process.
Firstly, I download a picture from the Internet.Then I named it "map.jpg",and copy to my workspace.latter,I create a M_files and type the code into the files.
for example:
function y=mean_data(gray)
s=size(gray);
sum=0;
for i=1:s(1)
for j=1:s(2)
sum=sum+gray(i,j);
end
end
y=sum/(s(1)*s(2));
Finally,the difference happenend:
if I call the function in this way:
I=imread('map.jpg');
J=rgb2gray(I);
mean=mean_data(double(J))
the result will be OK.
However if I call in this way:
I=imread('map.jpg');
J=rgb2gray(I);
mean=mean_data(J)
the result will be zero.
So why does the result being so different?And thank you for helping me!!!
This is becuase the default output format of the data read by imread
is uint8 that is - 8 bit per R/G/B. With 8 bit you can't get any integer
higher than 255. Take a look:
>> uint8(250) + uint8(5)
ans =
255
>> uint8(250) + uint8(6)
ans =
255
So then, during division in your function this thing happens:
>> uint8(255) / 12345
ans =
0
However, when you use double() you change the representation of
your data to 64 bit floating point - a lot more room for representing big
numbers.
Instead of using your loop function you can use matlab's mean
function - it works well with uint8 format:
>> mean(uint8([255, 231]))
ans =
243
So you can use:
mean_dat = mean(mean(J));
% it is also not a good idea to name a variable 'mean'
% if you are going to use the mean function so I renamed
% your variable to mean_dat

SSRS 2005 Round(foo, 0) is rounding a decimal value of 12.6 to 12 instead of 13

I have a report field that can contain different types of data, therefore I need to do my rounding and formatting using functions.
In one case, I have a decimal(9,2) value storing hours that I need to display as minutes with zero decimal places.
Round(foo*60, 0), where foo = .01 (which is 0.6 when multiplied by 60), is outputting 1 like I would expect.
Round(foo*60, 0), where foo = .21 (which is 12.6 when multiplied by 60), is outputting 12 instead of 13.
Why? How can I get it to consistently use standard 4/5 rounding?
Take a look at this other stackoverflow question:
SQL Server Reporting Services Round() function
You are seeing Round to Even logic rather than mid point rounding.
This Wikipedia article has all the details about rounding:
Wikipedia Rounding Article
I can't reproduce your results. Perhaps you are hiding some other relevant details.
Declare #foo Decimal(9,2)
Set #foo = 0.21
Select Round(#Foo * 60, 0), #foo * 60
The output from the above code is 13.00 and 12.60 respectively.
Is it possible that the Foo column is a float, or that there is more precision in the foo column that you expect? For example, the following code produces 12 (instead of 13).
Declare #foo float
Set #foo = 0.208
Select Round(#Foo * 60, 0), #foo * 60, Round(#foo, 2)

How to convert 4 char string, "1995", to decmial 1.9?

I am displaying a large number of vehicle records (120k+), each record has an engine size for that vehicle. The engine size can either in one of two formats:
"1.8"
OR
"1995" #cc's
If the engine size is saved as a 4 char string I want to abbreviate in the view, to the nearest 100th - for example "1995" should get displayed as "2.0" and "1900" should get displayed as "1.9".
What is the best way I can do this? (cannot update database - this is view logic only)
Thanks!
(size.to_f / 100).round / 10.0
I don't know what other cases you have, but if you can separate the two cases by a simple comparison, it is
if size > 1000 # or whatever condition here
sprintf("%.1f", size.to_f / 1000)
else
sprintf("%.1f", size)
end
Try this:
size = (size.length == 4) ? (size.to_f / 100).round / 10.0 : size.to_f

Resources