Look at my simple math script
double s = 3/2;
NSLog (#"%.2f",s);
I get 1, it should be 1.50
How can I fix it?
double s = 3/2 means divide the int 3 by the int 2, and then cast the result (which is the int 1) to a double (giving you 1.0).
You want this:
double s = 3.0/2.0;
try double s = 3/2.0
both 3 and 2 are integers, so despite receiver being double, you still get an int result and it is then converted to double
3 and 2 are integers. So 3/2 is 1. You should try instead
double s = 3.0/2.0;
NSLog (#"%1.2f",s);
It will give you the right answer.
Related
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
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))
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.
I want to calculate a simple number, and if the number is not an integer I want to round it up.
For instance, if after a calculation I get 1.2, I want to change it to 2. If the number is 3.7, I want to change it to 4 and so on.
You can use math.ceil to round a Double up and toInt to convert the Double to an Int.
def roundUp(d: Double) = math.ceil(d).toInt
roundUp(1.2) // Int = 2
roundUp(3.7) // Int = 4
roundUp(5) // Int = 5
The ceil function is also directly accessible on the Double:
3.7.ceil.toInt // 4
Having first imported math
import scala.math._ (the final dot & underscore are crucial for what comes next)
you can simply write
ceil(1.2)
floor(3.7)
plus a bunch of other useful math functions like
exp(1)
pow(2,2)
sqrt(pow(2,2)
I have a method that receives a number in a NSString format.
I wish to convert this string to a double which I can use to calculate a temperature.
Here's my method.
NSString *stringTemp = text; // text is a NSString
NSLog(#"%#",stringTemp); // used for debugging
double tempDouble = [stringTemp doubleValue];
NSLog(#"%f",tempDouble); // used for debugging
Please note I put the NSLog commands here just to see if the number was correct. The latter NSLog returns a value of 82.000000 etc. (constantly changes as it's a temperature).
Next I wanted to use this double and convert it to a Celsius value. To do so, I did this:
double celsiusTemp = (5 / 9) * (tempDouble - 32);
Doing this: NSLog(#"%d", celsiusTemp); , or this: NSLog(#"%f", celsiusTemp); both give me a value of 0 in the console. Is there any reason why this would be happening? Have I made a stupid mistake somewhere?
Thank you for your help!
Try doing (5.0 / 9.0). If you only use an int to do math where you are expecting a double to be returned (like 0.55) everything after the decimal place will be lost because the cpu expects an int to be returned.
5 / 9 is the division of two integers, and as such uses integer division, which performs the division normally and then truncates the result. So the result of 5 / 9 is always the integer 0.
Try:
double celsiusTemp = (5.0 / 9) * (tempDouble - 32);
If you evaulate (5/9) as an integer, then it is just 0.