Issue in converting string to double [duplicate] - swift

This question already has answers here:
swift: issue in converting string to double
(2 answers)
Closed 3 years ago.
I have mobile bank app.
When user type amount, then convert string to double i have problem
user typed amount example "8.7" is 8.699999999999999 and when i send request it sending 8.699999999999999
what can i do, to fix it?
I have tried this post:
swift: issue in converting string to double
var amount = "8.7"
var amountDouble = Double(amount)!
var amount = "8.7" . //"8.7"
var amountDouble = Double(amount)! //8.699999999999999

This imprecision is exactly why Double isn't an appropriate datatype for financial domains. Use Decimal instead, which is have perfect precision within its legal range.

Related

With Swift How Can I Convert String To Double one-to-one [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
swift: issue in converting string to double
(2 answers)
Swift double to string
(16 answers)
Closed 22 days ago.
While converting the string to double, I cannot do exactly the same. how can i fix this
StringValue = "0.00001174"...DoubleValue = Double(StringValue)...
print(DoubleValue) ->"0.000011749999999999999"
How can I get same . Thanks

Convert date and time in dart [duplicate]

This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
How do I format a date with Dart?
(24 answers)
Closed 9 months ago.
from API I am getting this:
"2022-05-12T15:55:03.000000Z"
My question is, how can I make it:
12.05.2022, 15:55
My question is, is here any easy way? Otherwise I can slowly and partially convert it with string methods, but I guess there must be better, more proper way?
Thank you in advance
you can user DateFormat to get a date as string formatted the way you want
String x = "2022-05-12T15:55:03.000000Z";
DateTime y = DateTime.parse(x);
final DateFormat formatter = DateFormat('dd.MM.yyyy, HH:mm');
final String formatted = formatter.format(y);
print(formatted); // 12.05.2022, 15:55

In Dart, how do you set the number of decimals in a double variable? [duplicate]

This question already has answers here:
How do you round a double in Dart to a given degree of precision AFTER the decimal point?
(28 answers)
Closed last year.
I want to set a double, let's call it Price, in Dart, so that it always gives me a double of 2 decimal places.
So 2.5 would return 2.50 and 2.50138263 would also return 2.50.
The simplest answer would be double's built-in toStringAsFixed.
In your case
double x = 2.5;
print('${x.toStringAsFixed(2)}');
x = 2.50138263;
print('${x.toStringAsFixed(2)}');
Would both return 2.50. Be aware that this truncates (e.g., 2.519 returns 2.51). It does not use the standard rounding (half-even) banker's algorithm.
I recommend using a NumberFormat from the intl package; The parsing and formatting rules are worth learning since they appear in other languages like Java.
double d = 2.519;
String s = NumberFormat.currency().format(d);
print(s);
returns USD2.52
s = NumberFormat('#.00').format(d);
returns 2.52
Since your are dealing with money, you should probably use NumberFormat.currency, which would add the currency symbol for the current locale.
Your question is more about how Dart handles the type double. Something like the following might work depending on your use-case:
void main() {
double num = 2.50138263;
num = double.parse(num.toStringAsFixed(2));
print(num);
}
More info about how Dart handles double can be found here.

How to stop rounding in NSDecimalNumber? [duplicate]

This question already has answers here:
In Swift 3, how to calculate the factorial when the result becomes too high?
(2 answers)
BigInteger equivalent in Swift?
(6 answers)
Closed 3 years ago.
I am solving a question from HackerRank which asks to print the value of extra-long factorials that can't be stored even in a 64-bit long variable.
I am using NSDecimalNumber to store the value. However, even in this case, the final result is rounded off.
func extraLongFactorials(n: Int) -> Void
{
var factorial: NSDecimalNumber = 1
for index in 1...n
{
let indexInNSDecimal = NSDecimalNumber(value: index)
factorial = factorial.multiplying(by: indexInNSDecimal)
}
let factorialWithoutRounding = factorial.description(withLocale: nil)
print(factorialWithoutRounding)
}
print(extraLongFactorials(n: 45)) // 119622220865480194561963161495657715064000000000000000000
However, the result should be 119622220865480194561963161495657715064383733760000000000.
This link talks about using description(withLocale:).
NSDecimalNumber round long numbers
However, it does not clearly explain how to use the description(withLocale:) method.
I also went through the apple doc https://developer.apple.com/documentation/foundation/nsdecimalnumber/1412789-description. But it also does not explain clearly how to use it.
Can someone please discuss this method in detail.

Round off a Double to Two Decimal Points [duplicate]

This question already has answers here:
Swift double to string
(16 answers)
Closed 5 years ago.
I've written a simple swift program to show how much it costs to run electrical devices. The program works fine (all be it a little clunky - I'm new to swift!) but the result shows several figures after the decimal point so I've attempted to round it off to two decimal places. I'm not having much luck! My code is:
var pricePerkWh: Double = 13.426
var watts: Double = 5.0
var hours: Double = 730.0
var KW: Double = watts/1000
var kWh: Double = KW*hours
var penceMonth: Double = kWh*pricePerkWh
var poundMonth:Double = penceMonth/100
var cost = poundMonth.roundTo(places: 2)
print ("It will cost £\(cost) per month")
From what I've read here, roundTo(places: 2) is used but this resulted in the error
error: Power Costs.playground:6:12: error: value of type 'Double' has no member 'roundTo'
var cost = poundMonth.roundTo(places: 2)
Any pointers would be greatly appreciated!
Thanks
Double indeed has no method roundTo(places:). That‘s a method you would first have to implement yourself.
To do that, see this answer, for example.
Or, if you don’t want to create a separate method, you could directly do this (inspired by the aforementioned answer):
let cost = (poundMonth * 100).rounded() / 100
BUT:
If you don‘t need the rounded value for any further calculations, but want to display it to the user, NumberFormatter is the way to go. For example, it also offers localization. See this answer.