Codename One Double with 2 Digits - double

I am having a double value with a constantly varying number of digits, like :
double d = 0.6645566;
double d = 0.664555666766;
double d = 0.66455887656655566;
I want this double to never have more than two digits, like:
double d = 0.66;
None of the solutions here seems to work, due to limitations in the CN1 APIs, I suppose.
Many thanks in advance for any kind answer.
How would I achieve this in Codename One?

You can use:
String twoDigits = L10NManager.getInstance().format(d, 2);
If you still want to keep the decimal you can do:
long val = (long)(d * 100);
d = ((double)val) / 100.0;

Related

How to avoid negative values in Flutter

double totTax = double.parse(nontax_amt) - double.parse(tax);
String tottax = totTax.toString();
double igst = double.parse(igstper) / 100 * double.parse(tottax);
double cAmt = double.parse(widget.itemDetailsList[position].cgstRate) /
100 *double.parse(tottax);
double total =totTax +igst +cAmt;
String totamt = tot.toString();
I am calculating some double values called totTax,igst,cAmt and it's giving values with negative sign. I want the positive answer instead of negative values, meaning whatever value I try to calculate it should not give answer with negative sign.
Note: Because of negative sign the value of total is becoming wrong.
Use abs() which converts any value to positive or absolute. Just like this,
String totamt = tot.abs().toString();
try with abs() method
double totTax = (double.parse(nontax_amt) - double.parse(tax)).abs();
double igst = (double.parse(igstper) / 100 * double.parse(tottax)).abs();
double cAmt = (double.parse(widget.itemDetailsList[position].cgstRate) /
100 *double.parse(tottax)).abs();

How to get fractional part in dart

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

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 .

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.

I cannot get decimal after doing a simple division math

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.