How can i insert a decimal point into either an integer or a String(probably a String), 2 decimals from the right? like with money?
eg/ 13922
139.22
How can i insert a decimal point into either an integer or a String(probably a String), 2 decimals from the right? like with money?
eg/ 13922
139.22
As described, the goal is to take an integer-like value (e.g. 13922) and present it as money (139.22). The thread already mentions inserting a dot into a string (, ) and using NumberFormat for display (). Those quick fixes work for simple output, but they are brittle for real applications.
Better practice is to keep storage and arithmetic numeric (store cents in a long or use BigDecimal) and do formatting only when producing output. This avoids floating-point rounding errors and makes calculations reliable. Example conversion and formatting (new approach):
long cents = 13922L;
BigDecimal dollars = new BigDecimal(cents).movePointLeft(2);
DecimalFormat df = new DecimalFormat("0.00");
String out = df.format(dollars); // "139.22"
Notes and edge cases to watch for: validate inputs (strip non-digits except a leading minus), pad short strings (so "5" becomes "0.05"), and handle negative amounts before inserting a decimal. For single-threaded code prefer StringBuilder if doing any string manipulation. For display use NumberFormat/DecimalFormat to respect locale and currency symbols; for calculations use BigDecimal with an explicit scale and RoundingMode when required. These steps keep presentation and money logic properly separated and reduce subtle bugs.
Jump to Post— mellowmike 9You can construct a stringbuffer from the string you want to modify, then use the insert method to insert a "." in the desired place. Then use the stringbuffer's toString() method to convert it back to a string. You can't put decimals in integers.
Jump to Post— cale.macdonald 4You could do as mentioned above (use the number format class).
However you could also do this using String functions (length, and substring)
int length = inputString.length(); String beforeDecimal = inputString.substring(0,length - 2); String formatted = beforeDecimal + "." + (inputString.substring(length - 2));
You can construct a stringbuffer from the string you want to modify, then use the insert method to insert a "." in the desired place. Then use the stringbuffer's toString() method to convert it back to a string. You can't put decimals in integers.
I've had a look at that class and thank you its exactly what im looking for but what syntax would i use to insert 2 from the right? i cant find it in the api
like String length and then -2?
Why don't you just use the package ......
import java.text.NumberFormat;
then use this in the body.......
NumberFormat money = NumberFormat.getCurrencyInstance();
lastly print out in the body .......
System.out.println(money.format(dollars));
Idk if this is what you were looking for, but this is a very simple version of getting the 2 decimal places.
You could do as mentioned above (use the number format class).
However you could also do this using String functions (length, and substring)
int length = inputString.length();
String beforeDecimal = inputString.substring(0,length - 2);
String formatted = beforeDecimal + "." + (inputString.substring(length - 2)); We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.