Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting strings to money

 
Giri moh'kar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
refering to the previous mails of subject "converting strings to money"

we have some fields like currency in our screen which stores currency as int/long data types at database level but at appliaction level,we are converting those int/long datatypes into strings as the currency is culture dependent data. For Example,different clients will use different operators(comma,Dot Operators)which gives different meaning for different cultures.

The following example will address the issue for a given Locale it will display the currency format. any comments for this?


//Example

//Parsing from string to double then formatting to currency basing on Locale.



import java.util.Locale;

import java.text.NumberFormat;



public class DemoCurrencyFormat{

public static void main(String args[])

{

NumberFormat numberFormat=NumberFormat.getInstance();

numberFormat.setParseIntegerOnly(false);

String str="1976.0826";

double d =Double.parseDouble(str);

// double userNumber=1976.0826;



numberFormat = NumberFormat.getCurrencyInstance(Locale.US);

System.out.println("User's Number(US):"+numberFormat.format(d));

}

}





/*
Output:

User's Number(US):$1,976.08

*/
 
Srikanth Basa
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check if this helps http://www.exampledepot.com/egs/java.text/Currency.html

Not an advanced question
[ September 13, 2007: Message edited by: Srikanth Basavaraju ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic