Trying to change this C++ to Java

//The following pseudocode describes how to extract the dollars and cents from a
//price given as a floating - point value.For example, a price 2.95 yields values 2 and 95
//for the dollars and cents.
//Assign the price to an integer variable dollars.
//Multiply the difference price - dollars by 100 and add 0.5.
//Assign the result to an integer variable cents.
//Translate this pseudocode into a C++ program.Read a price and print the dollars
//and cents.Test your program with inputs 2.95 and 4.35.

#include<iostream>

using namespace std;

int main()
{
    cout << "Enter a price: ";
    float price;
    cin >> price;

    int dollars = (int)price;
    float cents = (price - dollars) * 100;

    cout << "That is " << dollars << " dollars and " << cents << " cents.";

    return 0;
}
Member Avatar for iamthwee

Trying to change this C++ to Java

Or you could actually go to your classes and start paying attention.

commented: Nahh +0

It won't work in java any better than it will in C++.
tenths and hundredths can't be represented exactly in a binary floating point representation, so this naive approach will always suffer from the risk of rounding or truncation errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.