Grails overriding locale number format - parsing

I have a Grails application and I want to save a number with decimal places, e.g. 902.11. In my app, I use Czech locale, so the decimal point is represented as comma "," instead of point ".". But in the browser I want to do some calculations by Javascript, so the decimal point must be represented by ".".
Is there a solution so I can customize the Czech locale in my application so it will use the "." instead of ","? Or is there any other solution to this problem?

You should use the formatNumber tag and specify a locale that uses dots for decimal places, e.g.
<script ...>
var someNumber = <g:formatNumber number="${myNumber}" locale="en" />;
// ... do some javascript calculations
</script>

For reference:
formatNumber will work when you write the value out into the page, but Lojza probably wanted to get the number back to the controller which is not straightforward since the Grails data-binder is locale specific, so it will expect the number to be formatted according to the Czech locale instead of the base JS format.
You can either override the binder value converter which will be global for your application (may not what you want), or use a command object and retrieve the number value into a String attribute of the command object, so that Grails will not try to apply any number parsing on it. Then you manually convert this string into number according to the base JS format.

Related

How to display the dot of a decimal based on the locale but without being in a decimal number?

I have a calculator-like with obviously number buttons and a . button.
How can I display it depending on the local used knowing it's in the end more like a string than a decimal, so I guess I cannot use NSNumberFormatter for that.
Is there a way to do it without "translating" it for every language that I will use (which will be way less than there are locales)?
I thought of creating a formatted number like 0.1 using the locale, format it into string and keep only the . it its locale version but I guess there are proper ways
Thanks
Here is a way for it to work, I don't know if it's the best way...
let decimalSeparator = NSLocale.currentLocale().objectForKey(NSLocaleDecimalSeparator) as? String

User input money value in grails

I need to get a money value from the user, but the user can type the number in different formats:
1.234.234,78
1234566,26
123,123,132.12
I don't know how to treat the variable.
I have to transform that value in Double type but if the user give me a value with "," the program generate an exception, how can I handle this?
If you want to handle money then I recommend you a great grails plugin with many feature for currencies handling and conversions etc.
Have a look at Currency plugin.
In your domain static constraints use matches with the regular expression that handles currency format.
Then using java.text.NumberFormat allows you to format double regardless of having comma in the input.

#font-face: Icon fonts & Converting CSS character (Hex) Value

Background
I am working a lot at the moment with webfonts, and specifically icon fonts. I need to ascertain the which character a specific icon is for testing purposes, so I can simply type the character &/or copy-paste it.
Example
The CSS of most icon fonts is similar, using the :before pseudo approach e.g.
.icon-search:before{content:"\f002"}
Question
I believe this encoding to be called CSS character (Hex) is this the
correct?
Are there any tools that allow me to enter the escaped CSS character value and convert it to a value I can copy and paste
Is there a tool that can convert this to a HTML decimal value e.g. & = simple amperstand
Summary
I would love to be able to find out which character it is so I can simply type it on my keyboard. I have spent ages looking it up but am not quite sure what this type of encoding and conversion is called so can't find what i'm looking for. I'd appreciate some pointers.
SOLVED - the answer below for completeness
After some research myself I just want to confirm that the encoding used in CSS is indeed called HEX encoding.
I did find a converter that allows me to enter the HEX value and converts it to Decimal http://www.binaryhexconverter.com/hex-to-decimal-converter
If you want to use a HTML entity then all you need to do is wrap the converted decimal value in the obligatory &# ; entity start/finish characters and you are good to go.
Example
(HEXvalue = \f002) converts to (Decimal = 61442)
This HTML entity is therefore 

Stop NSString localizedStringWithFormat from putting comma

NSString localizedStringWithFormat puts in comma for separating thousands. How do you keep it from doing that. For example return 5000.25 instead of 5,000.25 (only have decimal separator)
localizedStringWithFormat uses the system's locale to format the numbers. What you want is to override the default locale - use initWithFormat:locale: for that. Basically, it's the same function, but you also supply the locale for which to format the text.
Also, keep in mind that initWithFormat:locale: also retains the string (as opposed to localizedStringWithFormat.
Check both of them here https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
Managed to get the desired behavior by using NSNumberFormatter and setting its groupingSize property to 0. Then used stringFromNumber method.

Using Razor view engine - how do I format a decimal value to have commas and two decimal places?

As the title suggests - I have a value in my viewmodel that is decimal. I have no control over that. I'd like to display it as currency using the Razor View Engine.
$#String.Format("{0:0.00}", 1005.3422)
gets me part way there with:
$1005.34
but how can I get the commas in there?
Thanks
Can you use {0:c} instead? This is just standard string formatting in .NET and the "c" is for currency. There are lots of standard numeric string formats. And, of course, custom formatting, too.
$ #String.Format("{0:#,##0.00}", 1005.3422)
Most of the time, when you don't get the character you're expecting with strings conversion, it can be a locale issue. For exemple, you're developing with a en-us locale, but someone comes with a fr-FR locale. Then the date, currency, etc will be formatted and parsed differently.
Ideally you'd be fetching the view data from some model e.g. public decimal ItemPrice { get; set; }
In which case the Razor expression in your view could be #Model.ItemPrice.ToString("c")
The same can be used for simple ViewBag items, e.g. #ViewBag.ItemPrice.ToString("c")
Or you can add at class definition
[DisplayFormat(DataFormatString = "{0:#,##0.00}")]

Resources