Updated code for pdf updation

This commit is contained in:
nisha
2024-10-28 22:15:25 +05:30
parent 98c1f425ac
commit d29ac8cc05
2 changed files with 42 additions and 17 deletions

View File

@@ -416,24 +416,50 @@ public class Utils {
return null;
}
}
public static String convertItalianAmountToDouble(String amount) throws NumberFormatException {
// Step 1: Remove the thousands separator and replace the decimal separator
String normalizedAmount = amount.replace(".", "").replace(",", ".");
// public static String convertItalianAmountToDouble(String amount) throws NumberFormatException {
// // Step 1: Remove the thousands separator and replace the decimal separator
// String normalizedAmount = amount.replace(".", "").replace(",", ".");
//
// // Step 2: Parse the string to a double
// double value = Double.parseDouble(normalizedAmount);
// if (value <= 0) {
// return amount;
// }
// // Step 3: Format the double to 2 decimal places
// DecimalFormat decimalFormat = new DecimalFormat("#.00");
// return decimalFormat.format(value); // Return formatted string
// }
// Step 2: Parse the string to a double
double value = Double.parseDouble(normalizedAmount);
if (value <= 0) {
return amount;
public static String convertToItalianFormat(String amount) {
try {
// Step 1: Sanitize and standardize the input
String sanitizedAmount = amount.trim();
if (sanitizedAmount.matches("\\d")) {
return sanitizedAmount;
}
// Step 2: Handle Italian-style input (e.g., "37.192,00")
if (sanitizedAmount.contains(".") && sanitizedAmount.contains(",")) {
// Assume the period is a thousand separator and the comma is a decimal separator
sanitizedAmount = sanitizedAmount.replace(".", "").replace(",", ".");
} else if (sanitizedAmount.contains(",")) {
// If the input contains only a comma, treat it as a decimal separator
sanitizedAmount = sanitizedAmount.replace(",", ".");
}
// Step 3: Parse the cleaned-up string into a double
double parsedAmount = Double.parseDouble(sanitizedAmount);
// Step 4: Format the parsed amount to Italian format
NumberFormat italianFormat = NumberFormat.getInstance(Locale.ITALY);
italianFormat.setMinimumFractionDigits(2);
italianFormat.setMaximumFractionDigits(2);
return italianFormat.format(parsedAmount);
} catch (NumberFormatException e) {
// In case of any issues with parsing, return a default or error message
return "Invalid amount format";
}
// Step 3: Format the double to 2 decimal places
DecimalFormat decimalFormat = new DecimalFormat("#.00");
return decimalFormat.format(value); // Return formatted string
}
public static boolean isItalianFormattedAmount(String input) {
// Regular expression to match Italian-style amounts (e.g., 41.003,00 or 123,45)
if (!input.contains(",")) {
return false; // Return false if there is no comma
}
String sanitizedInput = input.replace(",", "");
// Step 2: Check if the remaining string is a whole number