• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting float variable to double

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry guys,

hit a complete blank!



in this line

float moneyToKids = float myMoney / numberKids;

myMoney is a double
moneyToKids is a float

how do i convert myMoney to a float

i tried

float(myMoney) but it still gives an error

thanks
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should be "(float) myMoney", not "float(myMoney)". That's called a cast.

Given that money generally has at most two decimal places, you might as well make myMoney a float rather than a double.

In a real application, you would never use floats or doubles to handle money, though - that's what BigDecimal is for.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you want a float at all? Have you worked out how many digits of precision you might get and how many digits you actually have?
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:Given that money generally has at most two decimal places, you might as well make myMoney a float rather than a double.


The number of places after the decimal point is not relevant. For example, the value 17878654.45 has 10 significant figures and cannot be stored in a float without loss of precision.
Money is often represented as a long, specifying the number of cents. 1787865445 easily fits in a long.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
long won't work if you need fractions. Tim is right, in most real applications you would use BigDecimal.
 
reply
    Bookmark Topic Watch Topic
  • New Topic