I'd just like to know whether there's a bug in macOS or if I have to make changes in code with currency formatted TextFields.
My problem: just a simple TextField in SwiftUI like
TextField("amountOnDeadline",value: $startValue, formatter: Formatters.currencyFormatter)
The formatter ist pretty simple:
static let currencyFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 2
return formatter
}()
Build and run!
The offered field is displayed as currency field. Great! But when I enter a number like 123, the field is reset to 0.00 € (or the value displayed on start).
Only when I exactly mark the 0.00 without the currency symbol and enter a value, the new value is taken.
This is not very user friendly. Is it something I have to live with or is there a solution for this problem? e.g. a special config in NumberFormatter.
Instead of using NumberFormatter you can use currency(code:) FormatStyle with TextField init(_:value:format:prompt:).
TextField("amountOnDeadline",value: $startValue, format: .currency(code: Locale.current.currencyCode ?? "EUR"))
This will not reset the field to 0.00 even if you remove the currency symbol.
Related
I am trying to get the Minimum and Maximum Temperatures from Apple's WeatherKit to make a complication in my Weather app but Apple's Developer Documentation does not show how to get the value and nobody has asked this question before.
Here is the code I tried but didn't work:
Text("L: \(weather.currentWeather.lowTemperature) H: \(weather.currentWeather.highTemperature)")
// Text("L: \(Minimum Temperature) H: \(Maximum Temperature)")
Here is what I expected(i hardcoded this):
You get it with DayWeather
https://developer.apple.com/documentation/weatherkit/dayweather
var highTemperature: Measurement<UnitTemperature>
var lowTemperature: Measurement<UnitTemperature>
You can get it with .daily
let daily = try await service.weather(for: newYork, including: .daily)
https://developer.apple.com/documentation/weatherkit/weatherquery
I am trying to set an option for a specific graph in the decimal form. I went through many questions, but I can't seem to figure out why it isn't working for me.
var temp_chart_options = {
title: 'Temperature',
hAxis: {title: 'Date', titleTextStyle: {color: '#262626'}, format:'decimal' },
vAxis: {minValue: 0, format: 'decimal'},
keepInBounds: true,
};
temp_chart.draw(temp_data, temp_chart_options);
I tried doing format: 'decimal', or format: { pattern: 'decimal' } and even did temp_chart.draw(data, google.charts.Line.convertOptions(temp_chart_options)); and even looked at these questions:
google chart vertical axis and tooltip value formatting
Google Chart Vertical Axis and Tooltip Value Formatting
How to format numbers in Google API Linechart?
But none of them seem to work :(
EDIT
Does anyone know how to change the format of the hover?
The format parameter on your vertical axis needs to specify a proper format pattern. For example, vaxis: {format:'0.00'} will give you a number with two decimal places after it.
Note that Google's documentation is very incomplete about this. They give an example using the presets, and an example of using separators (e.g. '#,###'), but there is no example showing decimal places, nor is there any complete documentation on number format specifiers that I could find. Perhaps it is documented elsewhere, but the page I linked to above ought to at least provide a link to it if so. It does not. I discovered the 0.00 format by experimentation.
As an addition to the accepted answer, I needed to do a currency in GBP with 1000's separators and 2 decimal places.
The given Google 'currency' was unsuitable as it renders the currency symbol in your local currency.
I came up with this:
format: '£#,###.##'
Which could theoretically be used with any currency symbol.
This was the result:
I am trying to use it in my code after using DatePicker but no suggestions are coming for NSDateFormatter and only DateFormatter is coming.
If anyone has used in their code, kindly share it here.
Also, what changes should i make in order to receive the suggestions.
I want to change the format from MM-DD-YYYY to DD-MM-YYYY.
Swift 3 has dropped NS prefix, so you wont get suggestion for NSDateFormatter. DateFormatter is same as NSDateFormatter, just the name is changed. You can use DateFormatter in same way you have been using NSDateFormatter. Check SE-0086 proposal for more details.
try this
let dateFormatter = DateFormatter()
//format in which recived Date Present
dateFormatter.dateFormat = "dd-MMM-yyyy"
let newDate = dateFormatter.date(from: "20-MAR-2017")
// format to which you need your date to convert
dateFormatter.dateFormat = "MMM-dd-yyyy"
if let newDate = newDate{
let dateStr = dateFormatter.string(from: newDate)
print(dateStr) //Output: MAR-20-2017
}
So im a little new to tkinter and was making a gui. one of the problems i was having was that when i got a value from a entry widget. i could not get the labels that were associated with that entry widget to update after i had done some arithmetic to it. for example
var = tk.StringVar
entry1 = tk.Entry(root, textvariable = var)
entry1.pack()
then do something with var
label1 = tk.Label(root, text = var)
label1.pack()
i looked around before asking this and what i found was to connect the variables. i tried that but it still would not update after the first time.
How would i get the label to update everytime a new value was entered in entry. like if 2 was entered 4 would be displayed. then if i entered 5 then the label should update automatically and 10 should be dislplayed. in this case im just multiplying by two. the label should update until i close the program.
Thanks was having alot of trouble figuring this out any help is appreciated
def enter_hit(event):
do_something_with(var.get())
var = tk.StringVar
entry1 = tk.Entry(root, textvariable = var)
entry1.pack()
label1 = tk.Label(root, text = var)
label1.pack()
entry1.bind("<Return>",enter_hit)
When you hit Enter on entry1, then it calls enter_hit. Then var and it's text can be gotten, and you can do what you want with it to affect label1.
If this doesn't suit you, and you want it to change the moment a character is inputted, then try trace.
def traced_event(event):
do_something_with(var.get())
var = tk.StringVar
entry1 = tk.Entry(root, textvariable = var)
entry1.pack()
label1 = tk.Label(root, text = var)
label1.pack()
var.trace("w", traced_event)
The tooltips can be set to display percentages using the following code:
var formatter = new google.visualization.NumberFormat({
fractionDigits: 2,
suffix: '%'
});
formatter.format(data, 1); // Apply formatter to first column.
Is there a way for NumberFormat to multiply each element by 100? Otherwise the tooltip appears as .50%.
I am using vAxis.format = "format:'#%' " which does multiply by 100. So .5 is displayed as 50% on the vertical axis.
According to the documentation(icu-project.org/apiref), this can be overwritten by enclosing the % in single quotes, but this did not work.
The net result is that the tooltips do not match the axis. What is the best way to do this?
I got this working by specifying a formatter exactly as you do:
var chartData = google.visualization.arrayToDataTable(tableData);
var formatter = new google.visualization.NumberFormat({
fractionDigits: 2,
suffix: '%'
});
formatter.format(chartData, 1);
The 1 in the last call means the second column, in which I have float values.
Then I specify a format for the axis in the chart options, escaping the percentage sign as pointed out by documentation and others here:
var chartOptions = {
vAxis: { format: '#\'%\'' }
};
I then draw the chart:
var chart = new google.visualization.ColumnChart(document.getElementById('chart'));
chart.draw(chartData, chartOptions);
This renders a left side axis with values like 10%, 20% and so on. And the tooltips looks like the default one but with a percentage like 10.10%, 20.20% and so on.
If you want two fraction digits in the left side axis as well, use this as format in the chart options instead:
vAxis: { format: '#.00\'%\'' }
var formatter = new google.visualization.NumberFormat({
pattern: '#%',
fractionDigits: 2
});
Thanks to http://groups.google.com/group/google-visualization-api/
You must surround the percent (%) symbol itself in single quotes.
The line I used to do this looks like this: options['vAxis'] = {'format': "#,###'%'"};
Combining this with your formatter above, you can make the vertical axis have a percent symbol and also make the tooltip include it too.
Ok... So this is a little late. I admit I didn't need this seven years ago. Nevertheless, this worked for me.
var rows = data.getNumberOfRows();
for (var i = 0; i < rows; i++) {
data.setFormattedValue(i, 4, (data.getFormattedValue(i, 4)*100).toFixed(1) + "%"); //LY
data.setFormattedValue(i, 3, (data.getFormattedValue(i, 3)*100).toFixed(1) + "%"); //TY
}
In my case, I am using four columns, two of which are assigned to the right axis with percentages. I wanted those columns' tooltips to reflect the proper percentage rather than the decimal representation.
Here is a link to the Google docs:
https://developers.google.com/chart/interactive/docs/reference#DataTable_setFormattedValue
I hope this helps some random stranger looking for it. ;)