Yii Framework Forum: Handling decimal separators - Wiki article - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Handling decimal separators - Wiki article Rate Topic: -----

#1 User is offline   jpablo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 18-August 11
  • Location:Rosario, Argentina

Posted 23 January 2012 - 01:12 AM

Yii i18n does not cover decimal format. Some languages like Spanish (I live in Argentina) uses comma ',' instead of dot '.' as decimal separator.

A localized web app should:
1- Display decimal values with the local decimal separator.
2- Allow entering decimal values with the local decimal separator.

I posted a wiki article explaining a procedure to get it done. Wiki article

Please add your comments, suggestions, etc, here.
0

#2 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 23 January 2012 - 03:07 AM

Why not just using a custom formatter class? It would format values for display changing the separator and if you define static methods you can filter input. I've done that and it works like a charm. I'm storing decimal values (currency) with a fixed precision as integers (multiplied by 100) but one can store them in any float format.
Storing proper values in models also helps when doing any calculations.

Filtering this way helps validation anyway, since you can remove common typos before validation (or not).

BTW I'm doing the same with dates, filtering with date('Y-m-d',strtotime($date)) to allow almost any input format, not just Y-m-d that the DB requires.
Don't be a dick.
0

#3 User is offline   jpablo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 18-August 11
  • Location:Rosario, Argentina

Posted 23 January 2012 - 09:18 AM

Yes, a custom formatter class will work, but you need to declare the class in each grid, each form, etc. The CHtml override take care of the conversion system wide. Your application code will not change at all.

I'm curious, why are you storing decimal values as integer instead of DECIMAL?
0

#4 User is online   Da:Sourcerer 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,193
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 23 January 2012 - 09:45 AM

View Postjpablo, on 23 January 2012 - 09:18 AM, said:

Yes, a custom formatter class will work, but you need to declare the class in each grid, each form, etc.

Nope. You can just register your custom formatter in your config in the components-stanza. Gridviews, detailviews etc. will pick it up automatically ;)
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#5 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 23 January 2012 - 04:01 PM

View Postjpablo, on 23 January 2012 - 09:18 AM, said:

I'm curious, why are you storing decimal values as integer instead of DECIMAL?


I always considered such formats as an unnecessary hassle. AFAIR I've never needed it for anything other than monetary values with a fixed scale of 2. Easy to remember, no need to write it down in a schema definition.

Following the PostgreSQL manual (sorry for no link, it looks I'm too new here):

Quote

The type numeric can store numbers with up to 1000 digits of precision and perform calculations exactly. It is especially recommended for storing monetary amounts and other quantities where exactness is required. However, arithmetic on numeric values is very slow compared to the integer types, or to the floating-point types described in the next section.


I'm not saying it's wrong in any way, just my preference. Using decimals would hint any generators using db schema how to build validation rules, but it's not much effort to correct that by hand, especially if I do it once in a project lifetime. And I like to know where/when/how rounding happens when doing some calculations on user input.

A little digression - recently I learned that ROUND function in a popular spreadsheet software works different in cell expressions than in it's macro language. And I learned it the hard way - by debugging.
Don't be a dick.
0

#6 User is offline   jpablo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 18-August 11
  • Location:Rosario, Argentina

Posted 23 January 2012 - 04:15 PM

I understand your point of view on storing decimals in integer fields. I don't share it, but I understand it =)

View Postnineinchnick, on 23 January 2012 - 04:01 PM, said:

A little digression - recently I learned that ROUND function in a popular spreadsheet software works different in cell expressions than in it's macro language. And I learned it the hard way - by debugging.


Round and decimal precision is a hassle many times in many languages. Javascript, for example, just SUCKS handling decimal precision. Just try this:

Quote

num = 162.295;
num *= 100;
alert(num);


Javascript will show 16229.499999999998 instead of 16229.5 !!! So, be warned, handling decimal values in javascript is not safe at all. In fact, it's dangerous.
0

#7 User is offline   jpablo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 18-August 11
  • Location:Rosario, Argentina

Posted 23 January 2012 - 04:16 PM

View PostDa:Sourcerer, on 23 January 2012 - 09:45 AM, said:

Nope. You can just register your custom formatter in your config in the components-stanza. Gridviews, detailviews etc. will pick it up automatically ;)


Great! I'll try it in a couple of hours, then I'll update the wiki page. Thank you!
0

#8 User is offline   Miguel Garcia 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 06-November 12

Posted 20 November 2012 - 03:32 AM

Hi

I have problem with :
foreach($this->owner->getTableSchema()->columns as $name => $column)

owner not exist, and after, the gii fail with :
Missing argument 1 for ModelCode::getTableSchema(), called in D:\Proyectos\tuDulceTarta\web\commonend\helpers\CHtml.php on line 2168 and defined
0

#9 User is offline   tomvdp 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 06-November 12

Posted 14 June 2013 - 10:55 AM

View PostDa:Sourcerer, on 23 January 2012 - 09:45 AM, said:

Nope. You can just register your custom formatter in your config in the components-stanza. Gridviews, detailviews etc. will pick it up automatically ;)


Can you elaborate on that "etc" in your phrase ? It seems only gridviews and detailviews have this ability to format their output. It would have been nice that indeed any component could be instructed to use a formatter, but alas.

Why is there a 'type' property foreseen on a CDataColumn, but is it not possible to pass type information when creating for example an activeTextField so that it too would call the appropriate formatter ?

I am currently struggling to get the number formatting correclty for my locale (comma as decimal point).

Looking for a solution I found these:
http://www.yiiframew...of-dot-for-l18n
http://www.yiiframew...ators-and-i18n/
0

#10 User is online   Da:Sourcerer 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,193
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 17 June 2013 - 08:44 AM

View Posttomvdp, on 14 June 2013 - 10:55 AM, said:

Can you elaborate on that "etc" in your phrase ? It seems only gridviews and detailviews have this ability to format their output.

Hm, perhaps I've put that etc in there a bit too enthusiasticaly. In fact, I cannot remember any other places where this would be useful stante pede.

As for the solutions in the wiki: I think a behaviour for this would blend in more into Yii. Makes me remember I wanted to publish such a thing :)
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users