This question already has answers here:
Trouble with floats in Objective-C
(3 answers)
Closed 7 years ago.
I am getting an unusal issue with float in Objective C. I enter 100.1 and i get 100.100002 shouldn't it be something like 100.100000 .
Following is the code
float temp=100.1;
NSLog(#"%f",temp);
100.100000
Can someone guide me what am i doing wrong or how to fix it ? I cannot use fixed decimal places i-e i cannot just use 100.10 . I need all decimal places .
Because that is a fundamental part of what happens when you represent an arbitrary floating point value in binary. The number of binary digits is limited, therefore rounding occurs. Depending on your needs, you might be better off using NSDecimalNumber.
Try using double instead;
double temp=100.1;
NSLog(#"%.8f",temp);
100.10000000
It is an issue with representation accuracy. I do not think it will be a problem to use double instead.
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
When i try this on DartPad output is like this. Anyone can explain ?
This is expected behavior. Double numbers cannot represent all decimal fractions precisely, and neither 0.05 nor 42.05 are the exact values that the double values represent.
The exact values are:
42.0499999999999971578290569595992565155029296875
0.05000000000000000277555756156289135105907917022705078125
If you add these two exact values, the result can yet again not be represented exactly as a double. The two closest representable doubles are:
42.099999999999994315658113919198513031005859375
42.10000000000000142108547152020037174224853515625
Of these, the former is closer to the correct result of the addition, so that is the double value chosen to represent that result.
This issue is not specific to Dart. All language using IEEE-754 64-bit floating point numbers will get the same result, and that is probably all languages with a 64-bit floating point type (C, C++, C#, Java, JavaScript, etc).
This question already has answers here:
How to strip commas from float input?
(3 answers)
Closed 8 years ago.
I searched for a while for a ruby or rails method that I can call to convert a simple string like 1,000.53 into a float value but I couldn't.
All I could see is number_to_human which does the reverse of what I want. Is there anything available for my use case or am I alone (which I dont think). Also all I want is a simple conversion as above with commas and dot. No fancy currency or other any other notation conversion needed.
Thanks
EDIT
'1,000.5'.gsub(/,/, '').to_f works but I am looking for an already available method in ruby or rails. Or a better alternative to my solution with gsub.
First, remove all chars from the string that are not a digit or the separator (. in your example). Then call to_f on the sanitized string:
'1,000.53'.gsub(/[^\d.]/, '').to_f #=> 1000.53
to_f might work
Try "your_string".to_f
I am writing a Swift app and am dealing with decimals in a database (stored in mysql as decimals, with 2 digits. Basically it's sales someone made each day, so generally anything from $0 to $1000, but not millions, and nothing insane in terms of trailing decimals, just always rounded to 2 decimal places).
Referencing this helped me out:
How to properly format currency on ios
..But I wanted to just do a quick sanity check here and make sure this strategy is ok.
i.e I would use NSDecimal or NSDecimalNumber (is there a preferred swift equivalent??)
What would you all recommend I do when dealing with currency in Swift? I'd like to use the locale-based currency symbol as well. I have a class called Sales that contains the amount in question. What do you recommend the datatype to be?
Apologies if I am coming off lazy, I actually have some ideas on what to do but feel a little overwhelmed at the "right" approach, especially in a locale-sensitive way, and wanted to check in here with you all.
Thanks so much!
Update for Swift 3: A Decimal type is now available with built-in support for operators like *, /, +, <, etc. When used in an Any context (passed to Objective-C), it's bridged to NSDecimalNumber.
Old answer:
NSDecimal is not really supported in Swift (it's a weird opaque pointer type), but NSDecimalNumber is — and as in Obj-C, it's the best thing to use for base-ten arithmetic (because it actually does its operations in base ten). NSLocale, NSNumberFormatter and friends all work too and should satisfy your localization needs.
Swift 3 now has a Decimal (value) type which is bridged to NSDecimalNumber.
This question already has answers here:
Why is division in Ruby returning an integer instead of decimal value?
(7 answers)
Closed 8 years ago.
Should be Simple.
I'm using Rails and doing something along the lines of:
<% n = 15 / 2 %>
<%= n %>
However, whenever I output variable n, it doesn't give me a decimal.
Based on other calculations it looks like it's constantly rounding to floor, But I haven't added any sort of round method anywhere.
Could you explain why? I want it to actually round to ceil.
You can use Numeric#ceil
n = 15 / 2.0
n = n.ceil
Now, you need to either use .to_f or specify the numerator or denominator as a float ( 2.0 or 15.0) in order for it to be considered a floating point expression (as against an integer expression)
You asked for explanation as well - the explanation is that because there are no decimal points, ruby is treating your numbers as integers. When integer division is done in ruby, anything after the decimal point is truncated. In order to not lose that info, you need to tell ruby to do floating point arithmetic, which you can do in either of the ways Karthikr explained.
The issue is that 15 and 2 are not Floats but are Integers. You are doing Integer math.
You can coerce either to a float by using .to_f or adding that .0 and you will not have the same issue. If you want it to round up, then you can of course use .ceil
So, it isn't 'rounding to floor' it is a different concept.
This question already has answers here:
Dot Notation vs Method Notation
(6 answers)
Closed 8 years ago.
Total newb here.
What is the difference between this
_myUIProgressView.transform = CGAffineTransformScale(_myUIProgressView.transform, 1.0, 0.3);
and this:
[_myUIProgressView setTransform:CGAffineTransformMakeScale(1.0, 0.3)];
besides the brevity. Why would you favor one over the other?
Those 2 calls are functionally identical. The only difference is syntax.
The first is called dot notation. The second is a method call to the setter.
Dot notation is an alternative way to invoke a property's setter or getter, and it does exactly the same thing as the other syntax.
Some people (mostly old school C programmers) don't like the dot syntax. I'm an old C programmer, but I like it.