Fixing number of digits after comma - neo4j

I want to fix the number of digits after the comma (or decimal point) for a decimal result of a Cypher query:
match(n)
.
.
.
return(X)
How can I do this please?

As far as I know there is no way to round to a specific decimal precision so it you have to multiply to gross it up, round it and divide by the same number you multiplied by again to get back to the precision you desire.
Consider the following example to round to the nearest thousandth.
with 1.23456789 as dec_raw
return round(dec_num * 1000) / 1000 as dec_precision

Related

zig print float precision

In zig it is possible to print float values in decimal notation by using "{d}". This will automatically print the value at full precision. Is there way to specify the number of digits? Either for each value, or as some kind of global setting?
This will limit the number of digits after the decimal point, with rounding and zero-padding:
format(w, "{d:.1}", .{0.05}) == "0.1"
format(w, "{d:.3}", .{0.05}) == "0.050"
More info

Round down float number

I have the following float: 2.554 and I need to format as percentage using ruby/rails.
How to round down that float ?
I tried to use number_to_percentage but the value is displayed as 2.6 (round up)
You cant use the round method specifying the number of digits to round.
Sample
2.554.round(2)
=> 2.55
With the next variant you can force to round down if the third digit after comma is less than 5
2.555.round(2, half: :down)
=> 2.55

Why multiply two double in dart result in very strange number

Can anyone explain why the result is 252.99999999999997 and not 253? What should be used instead to get 253?
double x = 2.11;
double y = 0.42;
print(((x + y) * 100)); // print 252.99999999999997
I am basically trying to convert a currency value with 2 decimal (ie £2.11) into pence/cent (ie 211p)
Thanks
In short: Because many fractional double values are not precise, and adding imprecise values can give even more imprecise results. That's an inherent property of IEEE-754 floating point numbers, which is what Dart (and most other languages and the CPUs running them) are working with.
Neither of the rational numbers 2.11 and 0.42 are precisely representable as a double value. When you write 2.11 as source code, the meaning of that is the actual double values that is closest to the mathematical number 2.11.
The value of 2.11 is precisely 2.109999999999999875655021241982467472553253173828125.
The value of 0.42 is precisely 0.419999999999999984456877655247808434069156646728515625.
As you can see, both are slightly smaller than the value you intended.
Then you add those two values, which gives the precise double result 2.529999999999999804600747665972448885440826416015625. This loses a few of the last digits of the 0.42 to rounding, and since both were already smaller than 2.11 and 0.42, the result is now even more smaller than 2.53.
Finally you multiply that by 100, which gives the precise result 252.999999999999971578290569595992565155029296875.
This is different from the double value 253.0.
The double.toString method doesn't return a string of the exact value, but it does return different strings for different values, and since the value is different from 253.0, it must return a different string. It then returns a string of the shortest number which is still closer to the result than to the next adjacent double value, and that is the string you see.

Lua math ceil round up number when there is no digits after decimal point

I would like to round up the numbers by using math.ceil in Lua.
Some of the cases are make sense like:
ceil(260.5) -> 261
But some of the cases are weird like:
ceil(2.2*100) -> 221 # Suppose there is no round up and the answer is 220
I have no idea why it acts like this, what should I do if I would like to round up the number when there are digits after decimal point, and no rounding up if there is just an integer?
-- Update:
Thanks for the answering from #cyclaminist:
2.2 * 100 is actually a little larger than 220.0 because 2.2 can't be represented exactly as a floating point number. Try ('%.15f'):format(2.2 * 100): for me, it gives '220.000000000000028'
Ceil will return the integer which is the closest and not smaller than 220.000000000000028, so that 221 is returned.
The solution to get 220 is:
math.floor(2.2*100 + 0.5) -> return 220, Since math.floor return the closest but not larger than 220.000000000000028
Try tonumber(string.format("%.0f",2.2*100)).

Lua random number to the 8th decimal place

How do I get a random number in Lua to the eighth decimal?
Example : 0.00000001
I have tried the following and several variations of this but can not get the format i need.
math.randomseed( os.time() )
x = math.random(10000000,20000000) * 0.00000001
print(x)
i would like to put in say 200 and get this 0.00000200
Just grab a random number from 0-9, and slide it down 6 places. You can use format specifiers to create the string representation of the number that you desire. For floats we use %f, and indicate how many decimal places we want to have with an intermediate .n, where n is a number.
math.randomseed(os.time())
-- random(9) to exclude 0
print(('%.8f'):format(math.random(0, 9) * 1e-6))
--> '0.00000400'
string.format("%.8f",math.random())
to help anyone else. my question should have been worded a bit better. i wanted to be able to get random numbers and get it to the 8th decimal place.
but i wanted to be able to have those numbers from 1-10,000 so he is updated how i wanted it and the help of Oka got me to this
math.randomseed(os.time())
lowest = 1
highest = 7000
rand=('%.8f'):format(math.random(lowest, highest) / 100000000)
print(rand)
Hope this helps someone else or if it can be cleaned up please let me know

Resources