Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Implement ToString(String str,IFormatProvider) in java

807588Feb 12 2009 — edited Feb 12 2009
I have ToString(String format,IformatProvider format) in C# ,I have to implement this function in java .so how to implement ToStirng(String format,IformatProvider format) in java.


sample program:
public static void Main(string[] args)
{
Int32 abc = 1679;
string str2 = abc.ToString("D", System.Globalization.CultureInfo.GetCultureInfo("en-US"));
}

In C# D stand for decimal integer,C stand for Currency and many more like G,F,N,X etc
i have wriiten the wrapper in java like
public static String toString(Integer intValue,String pattern,Format form)
{
char[] ch = pattern.toCharArray();
switch(ch[0])
{
case 'd':
case 'D':
pattern="%d";
break;
case 'f':
case 'F':
pattern="%f";
break;
case 'g':
pattern="%g";
break;
case 'G':
pattern="%G";
break;
case 'e':
pattern="%e";
break;
case 'E':
pattern="%E";
break;
case 'C':
break;
case 'p':
case 'P':
pattern="%%";
break;
case 'x':
pattern="%x";
break;
case 'X' :
pattern="%X";
break;
default:
break;
}
Formatter f = new Formatter();

return f.format(pattern,intValue).toString();
//return f.format(pattern,str).toString();

}

My question is :

How to get the value of Locale.
Is this a write way or there is any other better way to migrate the C# ToString()..
Pleases share your suggestion ..
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 12 2009
Added on Feb 12 2009
2 comments
403 views