I have the following Problem.
I want to concat a String with an DoubleBinding to an String property.
Slider slider = new Slider(1, 5000, 500);
Label sliderValue = new Label();
sliderValue.textProperty().bind(Bindings.concat("Sleep: ", slider.valueProperty()));
This is working fine.
But I don't want to display the Double Binding as an Double, I want to round it to an int.
As you can see in the screenshot the Number is added as an Double to String.
Is there any way to do that, without creating an Listener?
Sry if my english it not correct. :P
You can use Bindings.format instead:
sliderValue.textProperty().bind(
Bindings.format("Sleep: %.0f", slider.valueProperty())
);
You can cast double to int:
sliderValue.textProperty().bind( Bindings.concat( "Sleep: ",
Bindings.createIntegerBinding( () -> (int) slider.valueProperty().get(), slider.valueProperty()) ) );
Related
Iam new to dart I want a Make table of food program which take an input from user for each food and how much they want but i have an problem with multiply listOfFood[name] with count which both of them double is there any way to solve this ?
double name , count ;
Map<String , dynamic> listOfFood = {
'bacon' :'4.2',
'salad' :'3.5',
'cheaken' :'5.6',
'Goatmeat' :'6.9',
'fish' : '6.5',
};
Map<int , dynamic> food = {
1 : listOfFood ['bacon']
,2 : listOfFood ['salad']
,3 : listOfFood ['cheaken']
,4 : listOfFood ['Goatmeat']
,5 : listOfFood ['fish']
};
stdout.write('please Choose your product \n ${listOfFood}');
name = double.parse(stdin.readLineSync()!);
count = double.parse(stdin.readLineSync()!);
double result = (food[name]) * count;
print(result);
}
You cannot coerce a String that looks like a number to a double in Dart.
Dart has strong typing even when you declare a variable dynamic.
In your case, you're taking the value from listOfFood (not a List, use a better name like priceByFoodName), which is always a String (despite the Map having dynamic values) and multiplying that with a double which can't work.
You should ask yourself why are the prices Strings?
If you want to use them as numbers, just make them numbers.
Also, you don't need dynamic at all. Use types! If you had done that the compiler would've told you right away what the problem was before you even ran the code. That's why types exist.
To solve the problem quickly... this line:
double result = (food[name]) * count;
Should be changed to:
double result = double.parse(food[name]!) * count;
But you should probably change your code quite a bit to handle errors, use appropriate types, stop ignoring nulls etc.
I have an Int variable defined like so...
var otpNo = Int()
Now, an integer value has been assigned to this variable.
Now I have passed this int value to another viewcontroller and in that viewcontroller, I want to assign this int value to a textfield. But I am not able to do so.
I have tried this...
Int(myTextField.text!) = otpNo
But I am getting this error message:
Cannot assign to value: function call returns immutable value
Try this:
You can assign text as follow :
myTextField.text = String(format: "%d", otpNo)
myTextField.text = "\(otpNo)"
You need to set the value like so:
myTextField.text = "\(otpNo)"
A UITextField's text property accepts String type. You will need to convert your Int into a String, instead of what you were trying in your question.
When you wrap a string in Int(), you're creating an immutable representation of that string, as an Int. You can't assign anything to that representation - it's just a number
myTextField.text = String(describing: optNo)
This converts the int to a string, and assigns it to the text property of that text field.
Very simple issue. I have the useless class:
class Useless{
double field;
Useless(this.field);
}
I then commit the mortal sin and call new Useless(0);
In checked mode (which is how I run my tests) that blows up, because 'int' is not a subtype of type 'double'.
Now, it works if I use new Useless(0.0) , but honestly I spend a lot of time correcting my tests putting .0s everywhere and I feel pretty dumb doing that.
As a temporary measure I rewrote the constructor as:
class Useless{
double field;
Useless(num input){
field = input.toDouble();
}
}
But that's ugly and I am afraid slow if called often. Is there a better way to do this?
Simply toDouble()
Example:
int intVar = 5;
double doubleVar = intVar.toDouble();
Thanks to #jamesdlin who actually gave this answer in a comment to my previous answer...
In Dart 2.1, integer literals may be directly used where double is expected. (See https://github.com/dart-lang/sdk/issues/34355.)
Note that this is syntactic sugar and applies only to literals. int variables still won't be automatically promoted to double, so code like:
double reciprocal(double d) => 1 / d;
int x = 42;
reciprocal(x);
would fail, and you'd need to do:
reciprocal(x.toDouble());
You can also use:
int x = 15;
double y = x + .0;
use toDouble() method.
For e.g.:
int a = 10
print(a.toDouble)
//or store value in a variable and then use
double convertedValue = a.toDouble()
From this attempt:
class Useless{
double field;
Useless(num input){
field = input.toDouble();
}
}
You can use the parse method of the double class which takes in a string.
class Useless{
double field;
Useless(num input){
field = double.parse(input.toString()); //modified line
}
}
A more compact way of writing the above class using constructor's initialisers is:
class Useless{
double _field;
Useless(double field):_field=double.parse(field.toString());
}
Since all divisions in flutter result to a double, the easiest thing I did to achieve this was just to divide the integer value with 1:
i.e.
int x = 15;
double y = x /1;
There's no better way to do this than the options you included :(
I get bitten by this lots too, for some reason I don't get any warnings in the editor and it just fails at runtime; mighty annoying :(
I'm using a combination:
static double checkDouble(dynamic value) {
if (value is String) {
return double.parse(value);
} else if (value is int) {
return 0.0 + value;
} else {
return value;
}
}
This is how you can cast from int to double
int a = 2;
double b = a*1.0;
I have function in which I create a div containing an onclick function, where I pass 2 variables. One is an int and the another one is a string. It's accepting the int and not the string.
var html ="";
var intC = 0;
var flightBI="";
var mileage=0;
flightBI = $(this).find("FLIGHT").text();
fnCreateDiv(){
html+="<div class='ui-grid-a' id='Details" + intC +"' onclick='fnConfirm("+intC+","+mileage+");' >";
html+="</div>"
}
fnConfirm(intc,mileage){
alert(intc);
alert(mileage);
}
I am getting an alert for both intc and mileage, since both are integers. If in the same case I pass the flight, I am not getting a value. This is what we usually do with JavaScript but is it different in jQuery?
intC and mileage are both initialized to 0 (int) so they both alert the same value.
See this jsFiddle with the fix
I too tried to find a solution for my question and found the escape sequence \" helped me out.
onclick='fnConfirm("+intC+","+flightBI+")
replaced as
onclick='fnConfirm("+intC+",\""+mileage+"\")
since i am adding html itself as a string.
I am displaying a double to the user, but it is printed as 1.00000000001
However, I need only two digits after the decimal point.
There's a class called Formatter that can do the trick. Here's a code snippet:
double value = 1.24790000001;
Formatter formatter = new Formatter();
String formatted = formatter.formatNumber(value, 2);
And, here's a link to the JavaDoc: javax.microedition.global.Formatter
Have you looked at String.format e.g.
String x = String.format("%.2f", number);
http://download.oracle.com/javase/6/docs/api/java/lang/String.html