I want to show the values displayed inside the bubbles as whole number Ints. For example: instead of "22.0" I just want "22".
The answer here doesn't work with the new iOS Charts because it requires an IValueFormatter instead of a NumberFormatter:
let numberFormatter = NumberFormatter()
numberFormatter.generatesDecimalNumbers = false
chartData.setValueFormatter(numberFormatter)
Error Message:
Cannot convert value of type 'NumberFormatter' to expected argument type 'IValueFormatter?'
Is there any way to solve this problem?
With the new charts, you still use numberformatter and then just convert at the end. Reference the code below.
let format = NumberFormatter()
format.generatesDecimalNumbers = false
let formatter = DefaultValueFormatter(formatter: format)
chartData.leftAxis.valueFormatter = (formatter as? IAxisValueFormatter)
Hope this helps!
Related
This question already has answers here:
How to convert Decimal to String with two digits after separator?
(2 answers)
Closed 5 years ago.
How to print formatted Decimal value in Swift? NumberFormatter works only with NSNumber, but Decimal is a struct. String(format:) doesn't work either.
Decimal is bridged with NSDecimalNumber, which is a subclass of NSNumber so NSNumberFormatter can handle it
let decimal = Decimal(11.24)
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
let string = formatter.string(from: decimal as NSDecimalNumber)!
I'm using ios Charts 3.0 on the objc project. I had searched the answers on the stackoverflow but no correct results for me. I'm using horizon bar chart and pie chart in my project and tried using
yAxis.valueFormatter = NSNumberFormatter()
yAxis.valueFormatter.minimumFractionDigits = 0
for every axis as well as the custom formatter with this :
- (NSString *)stringForValue:(double)value
axis:(ChartAxisBase *)axis
{
return [#((NSInteger)value) stringValue];
}
Both ways did not work for me. Could you guys please help to resolve this?
Try the code below. I wrote it at Swift and don't know much Objective-C. I hope it helps. I'm using this for BarChart and it works like a charm.
let fmt = NumberFormatter()
fmt.numberStyle = .decimal
fmt.maximumFractionDigits = 0
fmt.groupingSeparator = ","
fmt.decimalSeparator = "."
yAxis.valueFormatter = DefaultAxisValueFormatter.init(formatter: fmt)
You need to extend IValueFormatter protocol with custom formatter class,
import Foundation
import Charts
// Custom value formatter class
class BarValueFormatter : NSObject, IValueFormatter {
// This method is called when a value (from labels inside the chart) is formatted before being drawn.
func stringForValue(_ value: Double,
entry: ChartDataEntry,
dataSetIndex: Int,
viewPortHandler: ViewPortHandler?) -> String {
let digitWithoutFractionValues = String(format: "%.0f", value)
return digitWithoutFractionValues
}
}
Now in your code initialise bar value formatter using custom class. It will remove all precision/franction values.
let chartDataSet = BarChartDataSet(values: <dataSetValues>, label: <stringValue>)
let valueFormatter = BarValueFormatter()
chartDataSet.valueFormatter = valueFormatter
I'm connecting to an API and when I get data back for dates it looks like this:
2016-07-05T21:39:17.696Z
How can I set a uitextfield.text to that date so it looks like a normal date (ie. 07/05/2016 or something similar)?
The tricky part is the NSDate is returned as a string from the API so it's not in the NSDate format to begin with.
A text field is only for editing freeform text, not dates. You can go one of two routes. If you intend for the user to be able to edit a date, then use a UIDatePicker to provide an interface specifically for editing dates. If you just want freeform text back, then you can convert a date to a string with NSDateFormatter.
try:
let formatter = NSDateFormatter()
let yourDate = NSDate()
formatter.dateStyle = NSDateFormatterShortStyle
formatter.timeStyle = NSDateFormatterNoStyle
self.textfield.text = formatter.stringFromDate(yourDate)
I'm writing an app with swift like list app. When i create a new task everything is working fine but a want to write a creation time on Subtitle text of table view's cell. How can i do that?
You can use NSDate to get the current date and time.
So in your creation method, (or object init):
let date = NSDate()
let dateFormatter = NSDateFormatter().Shortstyle
let dateString = dateFormatter.stringFromDate(date)
I have an 'amount' input field that takes a number value that I wish to display as a currency within the input field itself. This I seem to be able to do with no issues.
I then use this value in a calculation and output as a currency value. Again this works fine the first time as it initially sees it as a double and not currency.
My problem comes when I try and reuse the value in the 'amount' input field, as the value is being seen and no longer a double because of the currency symbol etc.
Any suggestions?
Edited below based on suggestions:
var formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
var numberOfPlaces = 2.0
var multiplier = pow(10, numberOfPlaces)
var enteredAmount = formatter.numberFromString(finalAmount.text)?.doubleValue ?? 0.0
enteredAmountLabel.text = formatter.stringFromNumber(enteredAmount)
finalAmount.text = formatter.stringFromNumber(enteredAmount)
Using these amends the calculations work the first time anything is entered into the field. If you try a calculation when the field is populated it resets the field to $0.00
If I amend to:
formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
Everything works as expected but the amount input field no longer displays as currency.
I am trying to provide screenshots but I do not have enough reputation points.
Is this the line that gives you trouble?
var enteredAmount = NSNumberFormatter().numberFromString(finalAmount.text)!.doubleValue
It looks like you've got the right idea. You can use a number formatter to convert a value to a formatted string for display, or to convert a formatted string to a value.
I would expect that code to work.
Is that line giving you a compile error? An error at runtime? Not giving the value that it should?
EDIT:
#LeonardoSavioDabus pointed out the problem. That line should use the number formatter you created above:
var enteredAmount = formatter.numberFromString(finalAmount.text)!.doubleValue
And you really shouldn't use force unwrapping like that. You should use the nil coalescing operator:
var enteredAmount = formatter.numberFromString(finalAmount.text)?.doubleValue ?? 0.0
EDIT #2:
Here in the US, this code works perfectly in a playground:
import Foundation
var formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
let moneyString = "$12.93"
var enteredAmount = formatter.numberFromString(moneyString)?.doubleValue ?? 0.0
enteredAmount += 0.07
var newString = formatter.stringFromNumber(enteredAmount)
(I stripped out all the locale stuff to make it simpler.)
EDIT #3:
Your locale code doesn't make sense to me.
for identifier in ["en_US" , "en_UK" , "en_US","fr_FR"] {
formatter.locale = NSLocale(localeIdentifier: identifier)
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
}//end of locale detection for currency and decimal formatting
That code looks like it loops through 3 different values of identifier and assigns 3 different locales to your formatter, and the same max/min digit counts (both 2). The way I read that code, it's going to assign first the US locale, then the UK local, then US English again, and then finally French from France. So at the end of the loop the locale will be set to "fr_FR". That's probably not what you want.