Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK so you edit a text field using a numeric keyboard, which is dependent on the current locale, and thus get a text representation of a number, which is dependendt on the current locale, too. After editing has finished you read it and want to transform into a number. </p> <p>To convert you would use NSNumberFormatter like this:</p> <pre><code>NSNumberFormatter *nf = [[NSNumberFormatter alloc] init]; </code></pre> <p>You can setup the formatter as you will, setting locale (!), limits, formatting, decimal/grouping separator, number of decimal digits etc. Then you just use it:</p> <pre><code>float number = [nf numberFromString: field.text]; </code></pre> <p>And that's all! Now you have the number even if the text includes comma, provided you let both: keyboard and formatter, to have the same format, same style - i.e. probably just let current locale be used all over the place.</p> <p><strong>EDIT</strong></p> <p>this is a currency formatter, that can convert between string and number for currencies:</p> <pre><code>NSNumberFormatter *nf = [[NSNumberFormatter alloc] init]; [nf setNumberStyle: NSNumberFormatterCurrencyStyle]; [nf setRoundingMode: NSNumberFormatterRoundHalfUp]; [nf setMaximumFractionDigits: 2] </code></pre> <p>this is a percentage formatter with 4 decimal places:</p> <pre><code>NSNumberFormatter *nf = [[NSNumberFormatter alloc] init]; [nf setNumberStyle: NSNumberFormatterPercentStyle]; [nf setRoundingMode: NSNumberFormatterRoundHalfUp]; [nf setMaximumFractionDigits: 4]; </code></pre> <p>both in the current locale.</p> <p>As you see you can define the style, digits, rounding behaviour and much more, depending on numbers you are trying to enter. For more details (it is really a lot you can do with the NSNumberFormatter) you should read Apple docs, it would go beyond the scope of SO answer to describe it all.</p> <p>In your case, provided that paino and pituus are also UITextFields:</p> <pre><code>-(IBAction)calculate { NSNumberFormatter *nf = [[NSNumberFormatter alloc] init]; [nf setRoundingMode: NSNumberFormatterRoundHalfUp]; [nf setMaximumFractionDigits: 2]; float npaino = [[nf numberFromString: paino.text] floatValue]; float npituus = [[nf numberFromString: pituus.text] floatValue]; float x = npaino] / npituus *10000; label.text = [nf stringFromNumber: [NSNumber numberWithFloat: x]]; [nf release]; } </code></pre> <p>Now to avoid creating the formatter in each calculation you could make it an instance variable, since you need only one for those conversions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload