0

I'm trying to get a string parsed into a NSNumber that has the % character in the end where I can use the in the ShinobiChart value property of the series.

Given the following code, I'm creating the NSNumberFormatter:

var formatter = new NSNumberFormatter ();
            formatter.NumberStyle = NSNumberFormatterStyle.Percent;
            var locale = new NSLocale ("en_US");
            formatter.Locale = locale;

Now, I'll try to use the formatter to get the value for the chart slice label:

var value = formatter.NumberFromString (answer.RepliesShare.ToString () + "%");

This will give me the value 0.33... The point is, if I remove the "%" from the string, I'll get null on the value.

So, I need get a NSNumber that has the value of (i.e.) 30%(in other words, numberString+"%").

How can I get it?

To be more clear, I'm trying to achieve this:

ShinobiChart

Since ShinobiChart takes a Value of Type NSNumber and my RepliesShare is a decimal I'm trying just convert my decimal from 33.00 to 33%. The ShinobiChart don't take a string, otherwise I would have it handled. I found that NSNumber can be formatted with NSNumberFormatted so I thought I can use it since the Chart takes a number.

Gutemberg Ribeiro
  • 1,533
  • 1
  • 21
  • 45

3 Answers3

3

NSNumberFormatters do 2 things (amongst others):

  1. Translate a formatted numeric NSString into an NSNumber (@"25.4%" → @"0.254")
  2. Translate an NSNumber into a formatted NSString (@"0.254" → @"25.4%")

It's not terribly clear from your question, but I think you're trying to do the latter of these two options, but you're using the method for the first.

The following will take an NSNumber object and turn it into a %age formatted string:

var formatter = new NSNumberFormatter();
formatter.NumberStyle = NSNumberFormatterStyle.Percent;
formatter.Locale = NSLocale.CurrentLocale;
var value = formatter.stringFromNumber(new NSNumber(0.254));

This will result in value being an NSString representing 25.4%.

Provided that RepliesShare property on your answer object is an NSNumber then you can do the following to get the string you require:

var value = formatter.stringFromNumber(answers.RepliesShare);

Hope that helps.

Note: Rather than using new NSLocale ("en_US") here I used NSLocale.CurrentLocale. This will ensure that the resultant string is formatter appropriately for the current user's device. The most obvious effect that this will have with percentages is that in some European countries, a comma , is used as the decimal separator instead of a period ..

sammyd
  • 883
  • 5
  • 6
2

A SChartDonutSeries (and consequently SChartPieSeries) object has a LabelFormatString property. This takes a string and is used to format those labels. It has the same syntax as is used in NSNumberFormatter. Therefore, to get the behaviour you want, set the LabelFormatString after you've created the series object in your SChartDataSource subclass:

var series = new SChartDonutSeries();
series.LabelFormatString = "%0.2f%%";

This will format numbers as 2 decimal placed with a percentage sign on the end. i.e. 25.46225.46%. Adjust the format string appropriately to get the format you desire. Remember that to get a % sign to appear you need to escape it - %%.

Further details on creating an appropriate format string are available here: https://developer.apple.com/library/ios/documentation/cocoa/conceptual/Strings/Articles/formatSpecifiers.html

sammyd
  • 883
  • 5
  • 6
1

I think you're misunderstanding what NSNumberFormatter does. It has nothing at all to do with NSNumber objects.

NSNumberFormatter is an object for working with strings that happen to contain numeric values. It is not for working with NSNumber.

Since ShinobiChart takes a Value of Type NSNumber and my RepliesShare is a decimal I'm trying just convert my decimal from 33.00 to 33%.

You do not want to convert anything, just give 33.00 as a decimal type to ShinobiChart. I think Xamarin will convert your C# decimal to an NSNumber automatically.

If it doesn't work, try giving new NSNumber(33.00) to ShinobiChart. There should be no percents anywhere. You definitely do not want to create an NSNumberFormatter

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110