How to parse from string to Int without .0 in Karate [duplicate] - string

I have a floating point value of 0.99996 and want to round off to the nearest Integer. How do i do that in karate?

* def val1 = 0.99996
* print val1
* def val2 = Math.round(val1)
* print val2
Note that before Karate 1.0 you may need to do this to remove the decimal point:
* def val2 = ~~Math.round(val1)
Also refer to type-conversion: https://github.com/intuit/karate#floats-and-integers

Related

Calculation in Kotlin with Double data type gives unexpected result

The calculation below should result in "3.52" but outputs "3.5199999999999996". How can I fix this?
("4.52".toDouble() - 1).toString()
You can also use roundToInt() method to achieve the result as follows
val random = 4.52 - 1
val roundoff = (random * 100.0).roundToInt() / 100.0
println(roundoff) //3.52
You can use String.format() function like
String.format(Locale.US,"%.2f","4.52".toDouble() - 1)
The .2f means this argument should be formatted as a float and with a precision of 2 digits after the Decimal.

Validate the float value range in karate framework

My scenario is to check whether a field accepts range from 0.01 to 25000 .. it can be decimal value . What is way to do it in karate framework.. I used below regex which is not correct
regex [.0-9]*
Just multiply by 1 to convert a string to a number
* def foo = '0.2'
* assert (foo * 1) < 0.3
Also please read: https://github.com/intuit/karate#type-conversion

How to find the middle value of an integer without converting it to a string using Python3?

While solving the next smallest palindrome problem, I had to convert an integer to a string so I can reverse and find the middle value. There has to be a solution - how to find the middle value of an integer without converting it to a string using Python3? Appreciate your assistance. Thanks.
Examples:
input=78653, result=6
input=7564, result=5
This is a start. It needs some tweaking:
import math
def mid_digit(n):
# num of digits
a = math.trunc(math.log(n,10)+1)
# moving half of digits to the right of decimal point
b = n / 10 ** round(a/2 + 0.5)
# getting the left most decimal digit
c = math.trunc(math.modf(b)[0] * 10)
return c

String formatting according to number of decimal points

I want to write a string of variable values in a formatted way, according to the following:
Maximum decimal points is 3.
If there are less than 3 significant points than less are written.
For example:
the number 1.53848 will be written as 1.538
the number 1.0 will be written as 1 (rather than 1.000).
val variable1 = 1.
val variable2 = 1.53848
language = "%s average value is %.3f and %.3f".format(variable1, variable2)
This should do the trick:
def format(d: Double) =
BigDecimal(d).scale match {
case x if x > 2 => "%.3f".format(d)
case _ => d.toInt.toString
}
How about just removing the zeroes (and possibly the comma/separator character)?
def formatted(d: Double) = "%.3f".format(d).replaceAll(",?0+$", "")

How to convert a decimal to a integer number

In groovy if i have the code like this :
def num = 9
println mum/4
which outputs 2.25. But what I want is whenever I get a decimal like this I need that number to rounded to next int number in our case it should be 3. For example, if the result is 3.01 i need the output as 4. Can anyone say me how to do this in groovy?
You want the ceiling function. I believe it is Math.ceil.
couple of other options; if you declare your var as:
def num = 9
int a = num / 4
println a
Or you can use integer division:
println num.intdiv( 4 )

Resources