I don't see any way to round a number in Dart?
import 'dart:math';
main() {
print(Math.round(5.5)); // Error!
}
http://api.dartlang.org/docs/bleeding_edge/dart_math.html
Yes, there is a way to do this. The num class has a method called round():
var foo = 6.28;
print(foo.round()); // 6
var bar = -6.5;
print(bar.round()); // -7
In Dart, everything is an object. So, when you declare a num, for example, you can round it through the round method from the num class, the following code would print 6
num foo = 5.6;
print(foo.round()); //prints 6
In your case, you could do:
main() {
print((5.5).round());
}
This equation will help you
int a = 500;
int b = 250;
int c;
c = a ~/ b;
UPDATE March 2021:
The round() method has moved to https://api.dart.dev/stable/2.12.2/dart-core/num/round.html. All the above links are wrong.
Maybe this can help in specific situations, floor() will round towards the negative infinite
https://api.dart.dev/stable/2.13.4/dart-core/num/floor.html
void main() {
var foo = 3.9;
var bar = foo.floor();
print(bar);//prints 3
}
Related
Can I constraint a typedef to range of integers in Dart?
Like shown in this TypeScript SO answer
type MyRange = 5|6|7|8|9|10
let myVar:MyRange = 4; // oops, error :)
I would like to limit:
Dice dice = 0; // warning not compile
Dice dice = 1;
Dice dice = 2;
Dice dice = 3;
Dice dice = 4;
Dice dice = 5;
Dice dice = 6;
Dice dice = 7; // warning not compile
Like:
typedef Dice = 1|2|3|4|5|6
Is it possible in Dart somehow?
You can create a class that takes a set of allowed values and throws and exception if a value not in the set is assigned to its value member:
class Restricted<T> {
late final Set<T> _validSet;
late T _value;
Restricted(Set<T> validSet) {
_validSet = Set<T>.unmodifiable(validSet);
}
T get value => _value;
set value(T newValue) {
if (!_validSet.contains(newValue)) {
throw RangeError('$newValue is not a valid value');
}
_value = newValue;
}
}
And use it like
var r = Restricted<int>({1,2,3,4,5,6});
r.value = 2; // Okay
r.value = 0; // throws ValueError
See the code in action on DartPad
You are asking about a possible solution you have in mind, without sharing the actual problem.
If you state you real problem in more general terms, you may find a better answer here. Maybe an Enum is a better fit than a restricted int.
I did this:
var f = new Faker();
String.Join("", f.Random.Digits(10)
However, is there another method that would eliminate the 'Join' call?
Wow. Sorry for the late reply, but yes there's a way to do this in Bogus.
void Main()
{
var f = new Faker();
var numberString = f.Random.ReplaceNumbers("##########");
numberString.Dump();
}
OUTPUT:
2166951396
By the way, thanks for using Bogus!
This question already has an answer here:
why abs() function in dart return negative number when not wrapped in parenthesis?
(1 answer)
Closed 12 months ago.
void main() {
var x = -5.abs();
print(x);
}
Will be -5.
But:
void main() {
var x = -5;
print(x.abs());
}
Will be 5.
-5 is an object, and the result of -5.abs() will be connected to x. But we still see -5. Could you please correct me where I wrong?
I think what Dart does is:
var x = (-1)*5.abs();
You can use var x = (-5).abs(); to get 5
void main() {
var x = -5.abs();
print(x);
}
is almost certainly parsed as
void main() {
var x = -(5.abs());
print(x);
}
and since 5 is already positive, it comes back unchanged. And then you negate that result.
So guys how do we generate a random number between a range but that shouldnt contain a particular no. in that range in dart?
If you want to print random numbers from 0 to 999 except say, the number 100.
Then the following code fragment will be sufficient.
import 'dart:math';
void main() {
var n = 100;
do {
r = rng.nextInt(1000);
} while (r == n);
print(r);
}
Depends on requirements for time, and distribution of result, say you wish to preserve even distribution and want to avoid calling a new random number, and are using the range 0-2000 and filling in 100
import 'dart:math';
void main() {
var n = 100;
do {
r = rng.nextInt(2000);
}
if (r >= n){
r++
}
print(r);
}
Let me show u an example;
// created my method to get random numbers and use wherever I want
// used math library to create numbers randomly and equaled to numbers.
// now we have to check if it will contains 0 so we will call method again to create new random numbers list which one no including 0.
import 'dart:math';
int void randomNumbers()
{
int numbers=Random().NextInt(10);
if(numbers!=0)
{
return numbers;
}
else
{
int newNumbers= randomNumbers();
return newNumbers,
}
}
so u can call that method created below in anytime to anywhere.
Here is an example: We set a FlatButton and when pressed, the var "leftdicenumber" receives a random number betxeen 1 and 6:
FlatButton(
onPressed: () {
leftdicenumber = Random().nextInt(6) + 1;
},);
I want to assign two variables to integer and decimal parts on double.
how to do it?
One way would be
int x = abc.toInt()
int y = int.tryParse(abc.toString().split('.')[1]);
final double abc = 1.4;
int a = int.parse(abc.toString().split(".")[0]);
int b = int.parse(abc.toString().split(".")[1]);
Try this out, it should work fine