Rails: number_to_currency in Indian format

256 views
Skip to first unread message

Revath S Kumar

unread,
Sep 22, 2014, 7:40:51 AM9/22/14
to kerala-ruby...@googlegroups.com
Team,

number_to_currency convert a number into 450,500 format. Is there any way I can convert this into Indian format ie., 4,50,500


--
with regards,
Revath S Kumar,

    
     normal-1.png

Harisankar P S

unread,
Sep 22, 2014, 7:58:57 AM9/22/14
to kerala-ruby-users-group
This is a one liner that i found and used some time ago. Couldn't retrace from where i got the regular expression.

"RS #{number.to_s.gsub(/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/, "\\1,")}" # where number is the amount in your case 450,000


--
You received this message because you are subscribed to the Google Groups "Kerala Ruby Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kerala-ruby-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

deepak kumar

unread,
Sep 22, 2014, 8:50:19 AM9/22/14
to kerala-ruby-users-group
that's an awesome one liner didn't thought this using regular expression method
Regards
Deepak Kumar


Revath S Kumar

unread,
Sep 22, 2014, 8:53:33 AM9/22/14
to kerala-ruby...@googlegroups.com
Thanks hari,
Works perfectly. But can you enlighten me how that regex works?

Kevin Jayanthan

unread,
Oct 29, 2014, 7:02:02 AM10/29/14
to kerala-ruby...@googlegroups.com
Hello, did you understand how it works ?

Sathia S

unread,
Oct 29, 2014, 7:18:57 AM10/29/14
to kerala-ruby...@googlegroups.com

On Wed, Oct 29, 2014 at 4:32 PM, Kevin Jayanthan <kevin....@gmail.com> wrote:
Hello, did you understand how it works ?

--
Twitter: http://twitter.com/keralarb
Website: http://krug.github.io
IRC: #krug.rb, freenode
---
You received this message because you are subscribed to the Google Groups "Kerala Ruby Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kerala-ruby-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
------------------------------------------------------------------------------------------
Regards
sathia

Here I share my experiments with open source.

Kevin Jayanthan

unread,
Oct 29, 2014, 7:34:19 AM10/29/14
to kerala-ruby...@googlegroups.com
@Sathia,

It's not like that :D. I was just exploring the posts and I found this one, it seemed pending in the explanation part. So thought of explaining it lol.

Checking out their implementation anyway, thanks. ^^

Sathia S

unread,
Oct 29, 2014, 7:41:31 AM10/29/14
to kerala-ruby...@googlegroups.com
Oh. My Bad.

--
Twitter: http://twitter.com/keralarb
Website: http://krug.github.io
IRC: #krug.rb, freenode
---
You received this message because you are subscribed to the Google Groups "Kerala Ruby Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kerala-ruby-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Revath S Kumar

unread,
Nov 4, 2014, 12:58:50 AM11/4/14
to kerala-ruby...@googlegroups.com
I tried to understand that regex by reading some docs and all my finding are written as a blog post. 


Not sure how much this gonna help, but let see if anyone like to add something to it.

Vipul A M

unread,
Nov 4, 2014, 2:34:53 AM11/4/14
to kerala-ruby...@googlegroups.com
I sent out https://github.com/rails/rails/pull/17502 after this conversation. Do chime in, if you have any thoughts there.

Vipul A.M.
+91-8149-204995
Message has been deleted

Revath S Kumar

unread,
Nov 17, 2014, 11:22:37 AM11/17/14
to kerala-ruby...@googlegroups.com
Thanks kevin for the detailed write up. I will try to take a look & update my post ASAP.

On Tue, Nov 4, 2014 at 4:35 PM, Kevin Jayanthan <kevin....@gmail.com> wrote:
@Revath : I should really say that the post is really descriptive. Nicely presented. I think I can help in making certain parts clear (taking in consideration, that you mentioned you are writing what you understood), or maybe I have got it wrong. Anyway here they are :

"The Grouping start with ?= means its a positive lookahead and the previously captured match (matched by group 1) should follow match of this group. Whatever this Group 2 matches won't expand the match of Group 1. Whatever this Group 2 matches won't expand the match of Group 1."

I think it isn't a good idea to say that the previously captured match should follow the match of the positive lookahead group. Afaik, every consecutive group match is done right after the previous one. The match actually expands, i.e if the number is 45000, the whole match is 45000. But it will not be added to the actual MATCHED TEXT (this is the core speciality of lookaheads. should be mentioned instead).

"(?!\d) is a negative lookahead which succeeds when the regex inside lookahead fails. This helps to filter out last 3 digits of a number."

Imo, the regex inside lookahead succeeds means that there isn't any digit following the previous match. It fails means there is a digit following, then the negative lookahead should fail.
Also it isn't just last three digits of a number. It's to ensure that there are exactly odd number of digits remaining after the previous match. But you have given that idea clearly in the later parts.

"So with String#gsub we can replace the content of group 1 with <content of group1>,, ie., we can replace 12 with 12, and 34 with 34,."

Actually it's the MATCHED DATA that is being replaced not the content of 1st capture group.

Hope it helps.

--
Twitter: http://twitter.com/keralarb
Website: http://krug.github.io
IRC: #krug.rb, freenode
---
You received this message because you are subscribed to the Google Groups "Kerala Ruby Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kerala-ruby-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages