How to get fractional part in dart - flutter

is there any way to print out the fractional part of a double,
My double number,
4734.602654867
I want only 6026 from it.

There is a truncate() function for double type which returns the integer part discarding the fractional part. We can subtract that from the original double to get the fraction.
double myDouble = 4734.602654867;
double fraction = myDouble - myDouble.truncate();
print(fraction); // --> prints 0.602654867
Edit:
If we want 4 digits specifically from the fractional part, we can do this..
int result = (fraction*10000).truncate();
print(result); // --> prints 6026
To do all this one line, we can do it like this..
int result = ((myDouble - myDouble.truncate())*10000).truncate(); // <-- 6026

Something like
import 'dart:math' show pow;
var number = 4734.602654867;
var wantedDigits = 4;
var fraction = (number % 1 * pow(10, wantedDigits)).floor();
print(fraction);
should work.
Dartpad example.

You can do that using split()
Like this..
var s = 4734.602654867;
var a = s.toString().split('.')[1]. substring(0,4); // here a = 6026
Hope it solves your issue..

final double number = 4734.602654867;
final String result = number.toStringAsFixed(5).split('.').last.substring(0,4);

void main() {
double d = 4734.602654867;
print(((d - d.floor()) * 10000).floor()); // --> prints 6026
}
floor method returns the decimal part of the number

I got a simpler and straight forward answer, especially if you want to still use the fractional part as decimal, and not as a string
double kunle = 300.0/7.0; // the decimal number
int s = kunle.floor(); // takes out the integer part
double fractionalPart = kunle-s; // takes out out the fractional part
print(fractionalPart); // print the fractional part

Related

How to convert / round a double value to int or String in flutter dart

here is a double value
final dblValue = 25.5;
and I want this number as this int or string.
I tried the replaceAll function to remove the value after the ., but that didn't work too, I just need the number only without. value. how do I do that in dart
You can use the .floor() method which goes to the int value without the decimal points. For a formal definition:
The greatest integer no greater than this number.
final d = 25.9;
final d2 = 25.1;
int i = d.floor(); // i = 25
int i2 = d2.floor(); // i2 = 25
If you then want it as a string you can use the .toString() method on the int you just did.
To convert it to a string use
dblValue.toString();
To round it use
dblValue.round();
Or
dblValue.toInt();
And to floor the value use
dblValue.floor();
You'll need to use the .floor() method to round up.
final dblValue = 25.5;
int value = d.floor(); // value = 25

remove decimal in dart

I have a number: 466.62 as a double and can't change it to a string, how can I get rid of the decimal point and print 46662? I've managed to get 46662.0 by multiplying the value by 100, but I don't want to print the decimal point. Thanks
When you multiply 466.62 by 100, your end result will remain a double as well. This is why you are seeing 46662.0. So you need to convert it to Int value, so instead you should do it like this:
double vDouble = 466.62 *100
String vString = vDouble.toInt().toString();
This will give you 4662.
For a more generic case, use the String split method;
String str = '466.62';
//split string
var arr = str.split('.');
print(arr[0]); //will print 466
print(are[1]); //will print 62
After you have managed to get this 46662.0,
Use this :
46662.0.toStringAsFixed(0);
print(46662.0.toStringAsFixed(0)); - This gives 46662
print(46662.0.toStringAsFixed(0).runtimeType); - This gives as String.
Reference :
https://api.dart.dev/stable/2.8.4/dart-core/num/toStringAsFixed.html
You can do integer division by 1 and then turn it into a string
final String number = ((466.62 * 100) ~/ 1).toString();
Or just truncate
final String number = (466.62 * 100).truncate().toString();
More on the ~/ operator https://api.flutter.dev/flutter/dart-core/num/operator_truncate_divide.html
To remove decimals from use effective integer division
int x = (12345679.900 ~/ 1000)
~/ is effective integer operator and removes decimals .

Take decimal numbers only in dart/flutter

I have a number that I want to take only the decimal part and convert it to an integer with certain precision.
How can I do that in Dart Language or flutter ?
For example :
turn this 247.64646122587197 into this 6464
Drop the float number and take only 4 decimals and convert it to an integer.
Something like this should work:
((x % 1) * pow(10, 4)).floor()
you can also do that like this
void main() {
final double abc=247.64646122587197;
int y = int.tryParse(abc.toString().split('.')[1].substring(0,4));
print(y);
}
output :6464
You could try this:
return (247.64646122587197 * 10000).toInt() % 10000;
double value = 247.64646122587197;
double decimalValue = value - value.toInt();
print(decimalValue.toStringAsFixed(4))

Dart round a figure but as a double

I want to multiply a figure and then have it rounded to remove any decimal places but returned as a double, rather than an int since that is what I need.
I've tried this:
double number = 84.5;
double coke = (number * 2.3).round()) as double;
and this:
double coke = double.parse(number * 2.3).round());
But I can't get it to be a double, it just throws errors. The value would be 194.35 in this case and I want it to be 194.0.
double number = 84.5;
double answer = (number * 2.3).roundToDouble(); // 194.0
CopsOnRoad's answer is the most direct way, but in general the way to convert an int to a double is to use int.toDouble:
double number = 84.5;
double coke = (number * 2.3).round().toDouble();
Using as double doesn't work because as changes what type the object is treated as but doesn't change the underlying object.

How to get the first number after a decimal point of a Double?

I have a Double like this:
339.09965653839
How can I get the first number after the dot?
0 // desired result
I tried to look into similar questions but unfortunately I found only ways to separate a double in two parts also these two Double. I would like to have an Int and then be able to print it as a string.
To get the digit as an Int, you can do a little math:
let someNum = 339.09965653839
let digit = Int((someNum - Double(Int(someNum))) * 10)
And of course it is trivial to display digit in/as a string as needed.
Or, alternatively:
let number = 123.456
// 1234 % 123 = 4
let digit = Int(number * 10) % Int(number)
Convert double to string and fetch the first character after dot.
let value = 339.09965653839
let valueInString = "\(value)".split(separator: ".")
print(valueInString[1].first ?? "")