I display a reward which is a number with 9 decimals (must stay like this for the calculation)
But I would like to display only 4 decimal
My using a template in VueJS
<template>
<div class="mb-2">Reward: {{ myAcccount.reward.accrued.toFixed(4) * (10 ** -9) - appAcccount.reward.paid.toFixed(4) * (10 ** -9)}}</div>
</template>
But I cant applied toFixed() like this, do you know a way to do that directly in the template ?
Currently it diplay :
Reward : 0.0022680540000000003
So I would like
Reward : 0.0022
You can try applying .toFixed() to the end of the calculation by enclosing the math part inside ().
<div class="mb-2">Reward: {{ (myAcccount.reward.accrued * (10 ** -9) - appAcccount.reward.paid * (10 ** -9)).toFixed(4)}}</div>
toFixed() is returning a string and not a number.
For better readability I would extract the logic in an own function aswell
EDIT: added Math
instead of toFixed you could write a function which transform your number into a decimal.
const transformNumber = (number) => {
return Math.trunc(number * 10000)/10000;
}
and like mentioned a computed property:
computed: {
reward(): {
return transformNumber(myAcccount.reward.accrued) * (10 ** -9) - transformNumber(appAcccount.reward.paid) * (10 ** -9)
}
}
Related
I am having one usecase where I am calculating some values in js and trying to assert those using match. Datatype for comparison is double. Is there any way I can use match for comparing double in json with some delta provided?
For now, I have written custom javascript function which perform this comparison. But I am more inclined towards using match as it is very cleaner approach.
Yes this is easy, first read: https://github.com/karatelabs/karate#self-validation-expressions
And here is a step by step implementation:
* def valid = (x, y) => Math.abs(x - y) < 0.2
* assert valid(1, 1.1)
* assert !valid(1, 1.3)
* def response = { value: 1.5 }
* match response == { value: '#? valid(_, 1)' }
I wanted to make a shaking image in a imageView in Appcelerator TItanium
What I would like to do is to randomly change the top position and the left position of an imageView, to do like a shaking image.
OriginalImageLeft = image.left;
OriginalImageTop = image.top;
if (Math.random() > 0.5){
value = -1;
} else {
value = 1;
}
var viewAnimate = Ti.UI.createAnimation({
duration: 2000,
repeat:100,
left: OriginalImageLeft + (Math.random() * 20 * value),
top: OriginalImageTop + (Math.random() * 20 * value),
});
image.animate(viewAnimate)
But the code is not working, it calculates only one time the MathRandom() function so the shaking is not working.
Any idea ?
Create a function to calculate the position + doing the animation once. Then use the animation complete event to call that function again so it will calculate a new position + running the animation again. If you want to stop it just don't call the fuction again (e.g. add a counter/if-case around the function call).
A short info why your code won't work:
the OriginalImageLeft + (Math.random() * 20 * value) part is send to the native path once. So it will execute the Math.random() part in JS and send that number to the native App. The actual repeat part is executed with that calculated number.
A very basic question, What is the right approach to concatenate String to an Int?
I'm new in Kotlin and want to print an Integer value preceding with String and getting the following error message.
for (i in 15 downTo 10){
print(i + " "); //error: None of the following function can be called with the argument supplied:
print(i); //It's Working but I need some space after the integer value.
}
Expected Outcome
15 14 13 12 11 10
You've got several options:
1. String templates. I think it is the best one. It works absolutely like 2-st solution, but looks better and allow to add some needed characters.
print("$i")
and if you want to add something
print("$i ")
print("$i - is good")
to add some expression place it in brackets
print("${i + 1} - is better")
2. toString method which can be used for any object in kotlin.
print(i.toString())
3. Java-like solution with concatenating
print("" + i)
$ dollar – Dollar symbol is used in String templates that we’ll be seeing next
for (i in 15 downTo 10){
print("$i ")
}
Output : 15 14 13 12 11 10
You can use kotlin string template for that:
for (i in 15 downTo 10){
print("$i ");
}
https://kotlinlang.org/docs/reference/basic-types.html#string-templates
The Int::toString method does what you're looking for. Instead of explicit loops, consider functional approaches like map:
(15 downTo 10).map(Int::toString).joinToString { " " }
Note that the map part is even redundant since joinToString can handle the conversion internally.
The error you get is because the + you're using is the integer one (it is decided by the left operand). The integer + expects 2 integers. In order to actually use the + of String for concatenation, you would need the string on the left, like "" + i + " ".
That being said, it is more idiomatic in Kotlin to print formatted strings using string templates: "$i "
However, if all you want is to print integers with spaces in between, you can use the stdlib function joinToString():
val output = (15 downTo 10).joinToString(" ")
print(output) // or println() if you want to start a new line after your integers
Just cast to String:
for (i in 15 downTo 10){
print(i.toString() + " ");
}
You should use the $ . You can also use the + but it could get confusing in your case because the + has is also an operator which invokes the plus() method which is used to sum Integers.
for (i in 15 downTo 10){
print("$i ");
}
I have the following sequence:
extend CONFIG_ADC_CLK ocp_master_sequence_q {
divide_by : uint(bits:4);
align_by : uint(bits:4);
body()#driver.clock is {
var div : uint(bits:3);
case divide_by {
1 : { div = 0; };
2 : { div = 1; };
4 : { div = 2; };
8 : { div = 3; };
16 : { div = 4; };
default : { dut_error(divide_by," is not a legal Clock division for ADC"); };
};
gad_regs.gad_clk_gen.clk_algn = align_by;
gad_regs.gad_clk_gen.clk_dev = div;
do WR_REG seq keeping {.reg==gad_regs.gad_clk_gen;};
};
};//extend CONFIG_ADC_CLK ocp_master_sequence_q {
In the test I use the sequence :
do CONFIG_ADC_CLK seq keeping {.divide_by== 3;.align_by==0;};
For some reason the compiler refer the number of the field divide_byas hex number instead of decimal.
How can I ensure that it will refer it as decimal?
This is not related to sequences and not related to how numbers are assigned to fields. It's just about how numeric values are formatted in printing and string operations. The actual value of a field has nothing to do with how it is printed.
By default, dut_error(), message(), out(), append() and other string formatting routines use the current setting of config print -radix. So, you probably have it set to HEX in your environment.
If you need this specific dut_error() to always use decimal format, no matter what the config setting is, you can use dec(), like this:
dut_error(dec(divide_by)," is not a legal Clock division for ADC");
By the way, when using the second variant of those routines, such as dut_errorf() or appendf(), you can determine the radix by providing the right % parameter, e.g., %d for decimals or %x for hexa, for example, the above dut_error() might be rewritten as:
dut_errorf("%d is not a legal Clock division for ADC", divide_by);
Here, you can also use %s, in which case the config radix setting is still used.
For generating the bingo ticket generator I need the shuffling array .
When I press a button I should retrive the values from array (ex. array(1,2,3,4,5,6,7,8,9)). I
If I retrive first five random value may be 2 5 7 4 8. If press the button again then it should retrive other than previously retrived value (ex. 1 3 9 6 7)
I don't know if you are allowed to modify your input but why not try something like this :
// passing your array as argument
// passing the total number you want to extract as argument
function getRandNumbers( a:Array, requested_numbers:Number ):Array
{
// verify we don't request to much numbers
if ( requested_numbers > a.length )
{
trace( "Not enought available numbers in array" );
return null;
}
results_array = new Array(); // create our output array
while( results_array.length < requested_numbers )
{
rnd = Math.floor( Math.random() * a.length );
results_array.push( a[rnd] );
a.splice( rnd, 1 ); // remove the random result
}
}
now you're sure your array will contain only non used numbers each time you will call getRandNumbers.