wood burning stoves 2.0*
The moose likes Java in General and the fly likes converting strings to money Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "converting strings to money" Watch "converting strings to money" New topic
Author

converting strings to money

Giri moh'kar
Greenhorn

Joined: Jun 11, 2007
Posts: 7
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

Joined: Jun 06, 2005
Posts: 241
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 ]
 
 
subject: converting strings to money
 
Similar Threads
My SCEA Part 1Study Notes
Remove Currency Format - Can i parse back to a double?
converting strings to money
3 Features of Java for Internationalization
k&b pp.502-503