ZendFramework 2: Locale Currency values in a form - forms

Is there any way to convert how a text input displays a currency in its value in correct locale number format from within the Form construct method or ViewHelper?
e.g.
value from database is 10000.50
In US or UK, is viewed as 10,000.50
In EUR is viewed as 10.000,50
I have been able to do the conversions in controllers, models and views, but haven't come across how do do it here.
Thanks
Aborgrove

You have the Zend\I18n component which ships formatters to format numbers. You can both use the NumberFormat for numbers in general and the CurrencyFormat for specifically currencies.
These formatters are living in the Zend\I18n\View\Helper domain, but are not dependant on the view actually. Therefore, you can just use them anywhere you want:
use Zend\I18n\View\Helper\CurrencyFormat;
$formatter = new CurrencyFormat;
$formatter->setLocale('en-US');
$currency = $formatter(1234.56, 'EUR'); // "€1,234.56"
$currency = $formatter(1234.56, 'USD'); // "$1,234.56"
$formatter->setLocale('nl-NL');
$currency = $formatter(1234.56, 'EUR'); // "€ 1.234,56"
You have to be aware of two things:
The number formatting style is depending on the locale, so supply the correct locale (or set the default locale with Locale::setDefault()).
The "EUR" or "USD" is only for the signs, and together with the locale will be prepended or appended to the number formatted
You can simply use the Zend\I18n\View\Helper\NumberFormat if you only want to format the numbers without any currency code. More information about the formatting is available in the manual.

Related

Change the date format for the showLastUpdateTime at the bottom of pages

I've added the
showLastUpdateTime: true,
line to my docusaurus.config.js, and the date is showing at the bottom of documents, but the format is US (which doesn't work for the majority of the world). I need it to be unambiguous, or not 'wrong' :D
Does anyone know how I can change this?
I don't believe you can explicitly set the format manually - it uses the default Intl.DateTimeFormat constructor under the hood - however you can manipulate it by setting the base locale of your site to something other than the default.
To do this, you need to leverage the i18n configuration - add this to your docusaurus.config.js:
i18n: {
defaultLocale: 'en-GB',
locales: ['en-GB']
}
Substitute en-GB for whatever your desired locale is, and you'll see the date show in that locale's format, so in the UK it's dd/MM/yyyy.

IReport BigDecimal format "R$ #,##0.00" Monetary Value in Charts

I have a JasperReports chart, In the report, the field $F{soma} is BigDecimal, at thedatabase MySQL is Decimal(19,2). I'm using this sql: select SUM(valor) as soma to get the field $F{soma}.
Printing just $F{soma} i get labels like : 1.500,20. Without format expression. What i need is to show labels like : "R$ 1,520.20".
Tried this:
new java.text.DecimalFormat("R$ #,##0.00").format(Double.valueOf($F{soma}))
But no success, so if someone can point me a direction, i'll be thankful.
Have not reputation to post images, but links bellow are about the field types..
Field in MySQL:
Labels being printed (without format expression)
If your $F{soma} is a BigDecimal field then just write
new java.text.DecimalFormat("R$ #,##0.00").format($F{soma});
Use an instance of NumberFormat that has the right Currency to format the value. If the default Locale is giving you a problem,
Use NumberFormat.getCurrencyInstance(Locale inLocale) with the Locale you want, e.g.:
NumberFormat.getCurrencyInstance(Locale.US);
Use NumberFormat.getInstance() and setCurrency() to set the Currency you want, e.g.:
NumberFormat f = NumberFormat.getCurrencyInstance();
f.setCurrency(Currency.getInstance(…));

Formatting a string in jaspersoft?

In my report I have a string field with some numeric value, for example 123102,6. I would like to display 123 102,60 in my report.
Formatting numbers is a bugger in Java in general but gets even trickier with Jasper.
If your report works with a Locale that provides the space as the number group separator (like, for example, Locale.FRANCE does), then all you need to do is specify #,##0.00 as the value of the Pattern property of your field.
If that is not the case, or you are using several Locales, or you are resolving them at runtime, then the simplest way to solve your problem is to provide this:
new DecimalFormat("#,##0.00", DecimalFormatSymbols.getInstance(Locale.FRANCE))
.format(Double.valueOf($F{your_field_name}))
as the value of the Text Field Expression property of your field.

Override language specific pattern in iReport

How can I override the language specific pattern in iReport? I have set the pattern #,##0.00 on a field with a Double value.
If the report is in english, I get the following example output.:
10,000.00
If the report is in german, I get the following example output.:
10.000,00
I need the output 10.000,00 for the english and the german report.
How can I realize that?
Do not set any pattern (make sure to remove it). Instead, in the TextFieldExpression field use
new java.text.DecimalFormat("#,##0.00", new java.text.DecimalFormatSymbols(java.util.Locale.GERMANY)).format($P{parameter1}).
(This is a string. If your textfield expression class is double, you can parse the result.)
This will format the number as in German locale for all languages.
The latest versions of iReport has a feature called Pattern Expression. In that you only need to specify a pattern string, and leave the rest of the field calculation the same. This way you can separate the data from the format, I think it keeps things cleaner, but it also gives you more control over the format at runtime.
Edit. An example was asked for, here it is:
https://gist.github.com/ilopez/9369809#file-so22141769-jrxml
The magic happens in this expression:
new DecimalFormat().toPattern()
You can specify report locale via the REPORT_LOCALE parameter see: Setting REPORT_LOCALE in IReport?

Symfony change value of days in form

I have a problem with the form in symfony.
I have English names of the days but I would like them to be in Polish. I changed the range of the years, but the names of the days I can't
-> add ('DateOfBirth', 'date', array (
'years' => range (date ('Y') - 100, date ('Y'))
))
Any clue?
You can pass option format to change the date format itself. In this way you can make that months would be integer numbers, not full names.
When formatting, the DateType takes locale as the default: \Locale::getDefault(). So you just need to change the default locale. It is set in the parameters.ini/parameters.yml file or set via the routing or in other ways if you support multiple languages.
I suggest to look for more details in the implementation class:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/DateType.php