in java i want to convert a double that represents a price in a string without a decimal…
- User
in java i want to convert a double that represents a price in a string without a decimal point. For example 12.50 should return 1250
Assistant
You can multiply the double by 100 and convert it to an integer to remove the decimal point:
double price = 12.50; String result = String.valueOf((int)(price * 100)); System.out.println(result); // Output: 1250However, be careful with floating-point precision issues. For more reliable results with currency, consider using
BigDecimal:import java.math.BigDecimal; double price = 12.50; BigDecimal bd = BigDecimal.valueOf(price); BigDecimal multiplied = bd.multiply(BigDecimal.valueOf(100)); String result = String.valueOf(multiplied.intValue()); System.out.println(result); // Output: 1250Or if you want a more robust method:
public static String priceToString(double price) { return String.valueOf(Math.round(price * 100)); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.