java - Formatting a number String according to format String -


i have string representing number (e.g. 1234.56) , string representing format (e.g. 1,234.56 or 1 234.56 or 1234,56...) , need format number string according format string. both strings given.

some code better underdstanding:

public static void main(string[] args) {     string number = "1000.0";     formatnumber(number, "1,234.56"); //should 1,000.00     formatnumber(number, "1 234.56"); //should 1 000.00     formatnumber(number, "1234,56"); //should 1000,00 }  public static string formatnumber(string number, string format) {     return ??? } 

whats best way achieve that?

thanks paul

these 3 lines should do.

string pattern = format.replaceall("\\d", "#"); decimalformat myformatter = new decimalformat(pattern); return myformatter.format(value); 

i writing heart, write if encounter problems


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -