Validate the float value range in karate framework - decimal

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

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.

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

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

How to generate 4 digit unique id like discord id like #8052

DECLARE #chars NCHAR(36)
SET #chars = N'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
DECLARE #result NCHAR(4)
SET #result = SUBSTRING(#chars, CAST((RAND() * LEN(#chars)) AS INT) + 1, 1)
+ SUBSTRING(#chars, CAST((RAND() * LEN(#chars)) AS INT) + 1, 1)
+ SUBSTRING(#chars, CAST((RAND() * LEN(#chars)) AS INT) + 1, 1)
+ SUBSTRING(#chars, CAST((RAND() * LEN(#chars)) AS INT) + 1, 1)
SELECT #result
i try this but it's too long Im using nodejs and posgresql
You can generate the id with your Node.js app. Do a random with Math
Math.random().toString(36).substr(2, 4) // You can change the `4` to change the length.
It will output a string like this: 92q6
In details:
Math.random() // 0.2433971674181521
Math.random().toString(36) // 0.ghlhdmdypp8bmx6r
The toString method of a number type in javascript takes an optional parameter to convert the number into a given base. If you pass two, for example, you'll see your number represented in binary. Similar to hex (base 16), base 36 uses letters to represent digits beyond 9. By converting a random number to base 36, you'll wind up with a bunch of seemingly random letters and numbers.
— Chris Baker
Since you are using nodejs, you could use a standard integer in the database, and convert it to base36 when showing it to the user. In javascript, the parameter to toString on a numer is the base:
a = 46656
b = a.toString(36) // b = "1000"
And if you want to convert it back from base36 to a number, use parseInt:
b = "1000"
c = parseInt(c, 36) // c = 46656
If you want the string to be 4 characters, generate a number between 46656 ("1000") and 1679615 ("zzzz").

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