Solved

NumberFormat problem in java

Posted on 2012-03-26
28
370 Views
Last Modified: 2012-08-13
Hi Experts...
I use a number formatter provide by java to reduce the number of decimal places to two positions after the decimal places.
I have a float variable whose value is 3.44567
and i use number formatter as shown:
Float num=3.44567;

NumberFormat fmt = new DecimalFormat("#0.00");
And convert it by fmt.format(num)

Later when i print the variable it prints 3.44 but when i pass this value to a stored procedure whose type is float and after saving it the number exists in the original format i.e 3.44567.

I dont understand that while printing on the screen it prints properly but when i see the database i get all the numbers even after two decimal places.
Can anyone guide how to rectify this.
0
Comment
Question by:gaugeta
  • 21
  • 5
  • 2
28 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37768555
fmt.format(num) produecs a String value just for the priniting purposes

It does not affect the number num itself
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768569
format method  takes the double or float number amnd returns String
the number itself is not affected by the method

http://docs.oracle.com/javase/1.4.2/docs/api/java/text/NumberFormat.html#format%28double%29
0
 

Author Comment

by:gaugeta
ID: 37768572
@for_yan:Thanks for the reply.
How can i really achieve this behaviour.
Please help...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768579
when you then pass num to stored procesdure - you pass original number which is reorenstented the way double nujhmedrs are re;resented in memory
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768588
I think - this is your oprtimal behavior - you want to stire double numbers with all their digots and manipulate them with all theor didgint and only then when you need to print in the last momemt then you wnat to format
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768596
If you want you of course can do the following:

float f = (new Float(fmt.format(num))).floatValue();

and then you'll loose your digits after the second - but this is not recommended by any means
0
 

Author Comment

by:gaugeta
ID: 37768600
All i need to do is to make sure that the numbers stored in the database do not have more than two numbers after the decimal point.
How can i achieve the same.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768606
Sometimes you can use database options to round flost numebsr when you slect them for printing.
Oracle jhhas simple finction

select round(my_num, 2) from table

but do it only if you sleect for printing
All calculations should be done on the full numbers and only then they should be formatted otherwise you may loose accuracy
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768609
You can do it this way:


 num = (new Float(fmt.format(num))).floatValue();
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768610
and then store num
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768624
>All i need to do is to make sure that the numbers stored in the database do not have more than two numbers after the decimal point.

keep in mind that that is not very sensibel requirement - database will store numbers still the way it wants to store floating numbers. so if you really want to store float numbers you rather store tham as they are with all didgits and the use rounding or dformatting when when you slect them
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768630
If you don't carea bout their numeric properties (don't want to add or subtract them aor calculete averages) then you format them - make strings of them and store them as text field in the database - then you are guranteed that you'll select them with all exactly same difists as you stored them
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768645
Otherwise you cannot control their internal representations in the databse and there is no guratntee after you slecte them back that even if you stored numbe
 f= 5.02;
pst.setFloat(1,f);

there is no guarantee that on slecet you'll not get

5.0199999999;

If you use round(col_name,2) in Orcale then you would be guarnateed to have 5.02, but for that you need not store it necesarriry as exactly 5.02 - your rounding will anyway happen on the select stage
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768658
In fact it is betetr not to fight thsi system and try to store in database two digits excatly,  but rather take advantage of this syaterem - and do all storage and calculations with float numbers as float and only on the priontout or display do actual formatting
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 47

Expert Comment

by:for_yan
ID: 37768664
Let me know if you have any further questions, this is something which is really important to understand
0
 

Author Comment

by:gaugeta
ID: 37768755
@for_yan:Thanks for the help.
I tried creating a new float variale and assigning the value from the formatted number using the following syntax: (new Float(fmt.format(f55UpRating))).floatValue()) .
I sent this value directly to db but still its having all the numbers after the decimal places.
Please help...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768766
You need to assign.
nothing happns with f55Uprating number here:
(new Float(fmt.format(f55UpRating))).floatValue()) .
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37768769
All i need to do is to make sure that the numbers stored in the database do not have more than two numbers after the decimal point.

Why do you want to do that? It will simply make any calculations you do with them less precise
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768771
You need to assign:

f55UpRating = (new Float(fmt.format(f55UpRating))).floatValue()) ;

System.out.println(f55UpRating);

then send
f55UpRating
to database
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768779
As I said - it all does not gurantee you taht wnhe you select you'll not get
like
5.019999999

even though you stored 5.02

That's why as I many times explaine - store as they are - only on printout and display format -just before dosplay, not before storage
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768793
Thisis compiled and tested

           float  f55UpRating =  3.44567f;
        NumberFormat fmt = new DecimalFormat("#0.00");
        f55UpRating = (new Float(fmt.format(f55UpRating))).floatValue() ;

        System.out.println(f55UpRating);

Open in new window


Output:

3.45

Open in new window

0
 

Author Comment

by:gaugeta
ID: 37768832
@for_yan:Even i get the same thing.
I tried assigning it to a variable and then sending to the db,but still has decimal numbers aftertwo decimal places.
I undersatnd that there is no guarantee that the same value will return but since this is a reqirement this needs to be done.
Is there any other way to do the same.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37768841
You'd need to use a different column type - string or int
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768853
As I wrote you before use string or varchar2 ior what ever it is in the database type that you are using
and store

String s = fmt.format(f55UpRating);

now store s in database

pst.setString(1, s);
0
 

Author Comment

by:gaugeta
ID: 37769271
@for_yan:I tried to insert usuing the fowllowing syntax:

String s = fmt.format(p2UpRating);
pst.setString(1, s);

com.sybase.jdbc2.jdbc.SybBatchUpdateException: JZ0BE: BatchUpdateException: Error occurred while executing batch statement: Implicit conversion from datatype 'CHAR' to 'FLOAT' is not allowed.  Use the CONVERT function to run this query.
The datatype  of the databse is Float.
How to retify this error?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37769278
in the colmun which is supposed to be float you acnnot store text/string - you need to add oine more column to the table which would contain stringh and you can store thete the number as string
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37769285
The problem is that the requirement to have  a number in the database number column with two decimal digits cannot have any sense. Number of decimal digits odf the number when it is stored in the float column of database or just in memeory of the computer is not defined; it is only when you print or display the number in certain fromat that you can talk about the number of decimal digits.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37769297
In database you can talke about the accuracy of that number - say up to second decimal digits - but this by no means would mean that it has two decimal digits in its representation - this formula woul mean that the value you obtained is no more than by 0.01 differs from the actual value. Idf that was the requirement - it does not mean that it will have tow decimal digits. Number fo decimal digits of the numeber in its internal representation is not defined; only its accuracy may be defined.

And you of course would not be able to store characters in the filed which is supposed to be the number, that is exactly because numebrs are stored and represented in quite different ways, not as strings representing numbers. To store in that way tyou need to define special column which will be a test/character column,  not the number
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now