• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

float to int

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I need to work with values that represent money, so I want to divide the input value by 100 and store it in int.
then do the calculations and result multiply by 100. (so I don't have to work with big decimal)

I just want to ask about the best way to parse float (initial value) to int
1:
float salary = 1254.93f;
float f = salary * 100;
int i = (int) f; // salary as integer

2:
String profit = (annualProfit < 0) ? "0.00" : String.format("%.2f", annualProfit);
int i = Integer.parseInt(profit.replace(".", "")); // salary as int
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I wouldn't start from there if I had the choice (why start with a float?), but I think I prefer the second option - format to String, then parse, because this handles any floating point rounding nastiness for you with no effort, whereas just casting to int will simply truncate the value.

Int will handle quite large values, but are you certain that you won't exceed its range? The usual way to manage money is with BigInteger.

YMMV.
 
pete reisinger
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks,
yeah, maybe you are right, maybe I should start with int and leave the conversion for the front end.
I thought that the second option is better (as you said, floating point rounding) but could be slower - which doesn't really matter (early optimization = evil)
 
Dave Lorde
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pete reisinger wrote:...
I thought that the second option is better (as you said, floating point rounding) but could be slower - which doesn't really matter (early optimization = evil)



In general, don't worry about performance until you have hard evidence that it is a problem. Pick the best option for the business requirement, and if there's a performance problem, you can optimize after running a profiler to find exactly where and what the bottleneck is.
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic