Hi guys, I'm returning this, it is similar to how you percieve dollars, $"32.95" etc. I calculate it in cents which is an int, but the problem is the second half cuts off the 10s of cents part if the number is less than that. eg/ 32.08. Any ideas ? i know i need an if but i cant think how to write it.

public String toString()
     {  
         return (cents / 100)+ "." + (cents % 100);
     }

Hi guys, I'm returning this, it is similar to how you percieve dollars, $"32.95" etc. I calculate it in cents which is an int, but the problem is the second half cuts off the 10s of cents part if the number is less than that. eg/ 32.08. Any ideas ? i know i need an if but i cant think how to write it.

public String toString()
     {  
         return (cents / 100)+ "." + (cents % 100);
     }

You can either do it by using if then else statement by using this

if((cents%100) < 10)
    return (cents / 100)+ "." + "0"+(cents % 100);
else
    return (cents / 100)+ "." + "0"+(cents % 100);

You can also you use a Numberformat class.

import java.text.*;
NumberFormat formatter = new DecimalFormat("0.00");
return (formatter.format(cents/100.0));
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.