473,378 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Problems with comma(,) seperator.

283 100+
I have to apply a format to my currency value.

The currency i have is-->
1402000
15000
10000 and so on.

I want to display it as--->
14,02,000
15,000
10,000

What i've done is,

Expand|Select|Wrap|Line Numbers
  1. String value = mfobj.getTotalCost();//getting the value
  2. int i = Integer.valueOf(value).intValue();//converting to int
  3. Locale india = new Locale("en","IN");//custom locale
  4. NumberFormat nf = NumberFormat.getInstance(india); 
  5. DecimalFormat df = (DecimalFormat)nf;//instantiate a NumFormat ,cast it to DecimalFormat
  6. df.applyPattern("##,##,###");//pattern applying
  7. //String output = df.format(i);
  8. //Currency curr = Currency.getInstance(india);
  9.  
  10. display it as <%=df.format(i) %>
  11.  
the values im getting is
1,402,000
15,000
10,000

How can i achieve the above combination?

Regards,

ajos
May 22 '08 #1
11 2162
JosAH
11,448 Expert 8TB
Straight from the API documentation:

The grouping separator is commonly used for thousands, but in some countries it separates ten-thousands. The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000. If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".
I find the last sentence a bit of a deficiency because it implies that what you want
can't be done.

kind regards,

Jos
May 22 '08 #2
ajos
283 100+
Straight from the API documentation:



I find the last sentence a bit of a deficiency because it implies that what you want
can't be done.

kind regards,

Jos
Thanks jos, yes i forgot to mention that ive read the docs and didnt find any way. If there is any way around for this, kindly advice.

Regards,

ajos.
May 22 '08 #3
JosAH
11,448 Expert 8TB
Thanks jos, yes i forgot to mention that ive read the docs and didnt find any way. If there is any way around for this, kindly advice.

Regards,

ajos.
I googled a bit but it seems that it can't be done without hacking; the only
reference I found was about this class: com.ibm.icu.text.NumberFormat but
I don't think you want to go that route. The standard NumberFormat or
DecimalFormat classes simply can't do what you want (which I find a bit
peculiar). Sorry, no cookbook solution from me.

kind regards,

Jos
May 22 '08 #4
ajos
283 100+
I googled a bit but it seems that it can't be done without hacking; the only
reference I found was about this class: com.ibm.icu.text.NumberFormat but
I don't think you want to go that route. The standard NumberFormat or
DecimalFormat classes simply can't do what you want (which I find a bit
peculiar). Sorry, no cookbook solution from me.

kind regards,

Jos
Thanks jos for your time and effort :).
Its a pity that we dont have enough support for custom locales(international formats) and more power in the pattern we want.

I guess i have to be (hesitantly)happy with what ive done for now.

Thanks and regards,

ajos :)
May 22 '08 #5
JosAH
11,448 Expert 8TB
Thanks jos for your time and effort :).
Its a pity that we dont have enough support for custom locales(international formats) and more power in the pattern we want.

I guess i have to be (hesitantly)happy with what ive done for now.

Thanks and regards,

ajos :)
What I find particularly silly is that pseudo-intelligently deciding that the grouping
size can be deduced from the rightmost grouping character in the format string.
Maybe Sun's bugbase know more about it ... I personally find it a design flaw,
not a feature. Suppose I want to do this: "#,##,###,####,##". I want it and I don't
want any stupic class that determines that the group size equals 2 ...

Of course a hack around it can be written but what do we have those format
classes for then? Oh well, Sun has done more stupider things ...

kind regards,

Jos
May 22 '08 #6
ajos
283 100+
......
Maybe Sun's bugbase know more about it ... I personally find it a design flaw,
not a feature. Suppose I want to do this: "#,##,###,####,##". I want it and I don't
want any stupic class that determines that the group size equals 2 ...

Of course a hack around it can be written but what do we have those format
classes for then? Oh well, Sun has done more stupider things ...

kind regards,

Jos
I know this is an old thread, just wanted to point out a bug report which i found today to save others from further searching.Bug

Hope That Helps,

ajos
Aug 6 '08 #7
JosAH
11,448 Expert 8TB
I know this is an old thread, just wanted to point out a bug report which i found today to save others from further searching.Bug

Hope That Helps,

ajos
Well, it only helps to show how narrow minded assumptions are. I still find it a
bit peculiar that the position of all but the rightmost comma are ignored. Why
put them in a format specification then? I read that this deficiency has a low
priority on Sun's list so I don't expect a fix anytime soon ...

kind regards,

Jos
Aug 6 '08 #8
blazedaces
284 100+
Well, it only helps to show how narrow minded assumptions are. I still find it a
bit peculiar that the position of all but the rightmost comma are ignored. Why
put them in a format specification then? I read that this deficiency has a low
priority on Sun's list so I don't expect a fix anytime soon ...

kind regards,

Jos
Is there a way to make it easier by doing something along the lines of extending DecimalFormat and overriding that specific set pattern method?

I can't tell if what I'm talking about is impossible...

-blazed
Aug 6 '08 #9
JosAH
11,448 Expert 8TB
I can't tell if what I'm talking about is impossible...
It's not impossible of course but it'd take almost a rewrite of that class. Have
a look at its source (in file src.zip in your jdk directory) and see for yourself.

kind regards,

Jos
Aug 7 '08 #10
ajos
283 100+
It's not impossible of course but it'd take almost a rewrite of that class. Have
a look at its source (in file src.zip in your jdk directory) and see for yourself.

kind regards,

Jos
Yes its a long standing issue which i think needs to be fixed(or may be rewritten?) considering Java is used widely all around. There is also a related bug if anybody noticed Here.

regards,

ajos
Aug 7 '08 #11
chaarmann
785 Expert 512MB
Can't you do something with regular expressions to get it formatted?
Escpecially if you have it as String already?
I haven't verified the code below, but I hope you get the idea.

Expand|Select|Wrap|Line Numbers
  1. String s = "1402000" // your number to be formatted
  2. Pattern p = Pattern.compile("(.{2})(.{2})(.{3})$");
  3. Matcher m = p.matcher(s);
  4. String formattedString = String.format("%s,%s,%s"), m.group(1), m.group(2), m.group(2));
Just make the regular expression pattern for the biggest number expected, in my case 7. If you have number smaller than 7 characters, for example "1234", fill the beginning with spaces and trim away leading spaces and commas afterwards:

Expand|Select|Wrap|Line Numbers
  1. String s = "       " +"1234"; // append 7 spaces in front, no matter how big the number is.
  2. ...
  3. formattedString=  formattedString.replace("^[ ,]+", "");
or you could try with
Expand|Select|Wrap|Line Numbers
  1. s.split("(^.{1,2}.{2}.{3}$)|(^.{1,2}.{3}$)|(^.{1,3}$)")
and append the pieces somehow. I think there is a nice functions in jakarta commons library to do it with a single command.
Aug 7 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: JKop | last post by:
void Blah(int k) { k; } int main() { int a; int b; Blah( ++a, ++b ); }
4
by: Jesper Denmark | last post by:
Hi, I use the double.Parse functionality to convert a number in a text file into a double. However, while this works fine on one computer it doesn't on another. I've found out that it is...
7
by: Sick | last post by:
My application populates a ListBox from a .TXT file. In the textfile there are prices with both dots as well as comma's for decimal indication. Example: 1234990; xg-tr-45; 1700,50; 0 2662666;...
6
by: Tommy DN | last post by:
I think I've a simple question. I'm using the datatype "decimal". I need to build a sql - insert statement to save something in the database. But the problem is that the decimal - datatypes are...
15
by: news | last post by:
I'm making a recipe database, and need to have DB fields in mySQL that will have lists of values that would get sent to an array to be worked on. I anticipate that at times a comma will need to...
19
by: M. Posseth | last post by:
Hi ,, i live in Europe in Europe the decimal seperator is , not a . why is it so that if i press on my numeric keypad the . a . appears ?? in my opinion it should be a , ( sounds odd but...
0
by: POIUY | last post by:
how to print a commaseperator using C#.net
6
by: Peter Larsen [] | last post by:
Hi, How do i convert a string to float/double if the decimal-seperator doesn't follow the local language settings ?? Eg. string s = load a value from a file - "1234,12"; float f =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.