Displaying Currency in Indian Numbering Format
Unfortunately on standard Java SE DecimalFormat
doesn't support variable-width groups. So it won't ever format the values exactly as you want to:
If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So
"#,##,###,####" == "######,####" == "##,####,####"
.
Most number formatting mechanisms in Java are based on that class and therefore inherit this flaw.
ICU4J (the Java version of the International Components for Unicode) provides a NumberFormat
class that does support this formatting:
Format format = com.ibm.icu.text.NumberFormat.getCurrencyInstance(new Locale("en", "in"));
System.out.println(format.format(new BigDecimal("100000000")));
This code will produce this output:
Rs 10,00,00,000.00
Note: the com.ibm.icu.text.NumberFormat
class does not extend the java.text.NumberFormat
class (because it already extends an ICU-internal base class), it does however extend the java.text.Format
class, which has the format(Object)
method.
Note that the Android version of java.text.DecimalFormat
class is implemented using ICU under the hood and does support the feature in the same way that the ICU class itself does (even though the summary incorrectly mentions that it's not supported).
How to display currency in Indian numbering format
In order to generate numbers in Indian currency format, you can use toLocaleString('en-IN')
. You need to change the input type of rFor1
to text.
Also, if it helps you can use keyup
event instead of click
.
<input name="" type="text" placeholder="" class="form-control input-md"value="0" id="rFor1" readonly>
$("#rTpe1").keyup(function(e){
$("#rFor1").val((this.value * $("#PerHourRate1").val()).toLocaleString('en-IN'));
});
Format a number in Indian currency format
I don't know of any native way to do this, but the following function will achieve it for you:
nums <- function(n) {
dec <- round(n %% 1, 2)
dec <- ifelse(dec < 0.01, "", substr(dec, 2, 4))
int <- n %/% 1
ints <- vapply(int, function(x) {
x <- as.character(x)
len <- nchar(x)
if(len <= 3) return(x)
rev_x <- paste(rev(unlist(strsplit(x, ""))), collapse = "")
str <- paste0(substr(rev_x, 1, 3), ",")
str2 <- substr(rev_x, 4, 100)
str2 <- gsub("(\\d{2})", "\\1,", str2)
rev_x <- paste0(str, str2)
return(paste(rev(unlist(strsplit(rev_x, ""))), collapse = ""))
}, character(1))
return(sub("^,", "", paste0(ints, dec)))
}
You can use it like this:
nums(c(1234.12, 342, 35123251.12))
#> [1] "1,234.12" "342" "3,51,23,251.12"
Displaying Currency in Indian Numbering Format in Swift3 IOS
You can do this:
let formatter = NumberFormatter() // Cache this, NumberFormatter creation is expensive.
formatter.locale = Locale(identifier: "en_IN") // Here indian locale with english language is used
formatter.numberStyle = .decimal // Change to `.currency` if needed
let asd = formatter.string(from: NSNumber(value: intString)) // "10,00,000"
P.S. Idk why Int
variable is called intString
but I've kept it for consistency.
How to display Currency in Indian Numbering Format in PHP?
You have so many options but money_format
can do the trick for you.
Example:
$amount = '100000';
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount;
Output:
1,00,000.00
Note:
The function money_format() is only defined if the system has strfmon capabilities. For example, Windows does not, so money_format() is undefined in Windows.
Pure PHP Implementation - Works on any system:
$amount = '10000034000';
$amount = moneyFormatIndia( $amount );
echo $amount;
function moneyFormatIndia($num) {
$explrestunits = "" ;
if(strlen($num)>3) {
$lastthree = substr($num, strlen($num)-3, strlen($num));
$restunits = substr($num, 0, strlen($num)-3); // extracts the last three digits
$restunits = (strlen($restunits)%2 == 1)?"0".$restunits:$restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping.
$expunit = str_split($restunits, 2);
for($i=0; $i<sizeof($expunit); $i++) {
// creates each of the 2's group and adds a comma to the end
if($i==0) {
$explrestunits .= (int)$expunit[$i].","; // if is first value , convert into integer
} else {
$explrestunits .= $expunit[$i].",";
}
}
$thecash = $explrestunits.$lastthree;
} else {
$thecash = $num;
}
return $thecash; // writes the final format where $currency is the currency symbol.
}
Related Topics
Java Date() Giving the Wrong Date
What's the Difference Between Getpath(), Getabsolutepath(), and Getcanonicalpath() in Java
What Is the List of Valid @Suppresswarnings Warning Names in Java
Using Mockito with Multiple Calls to the Same Method with the Same Arguments
Send an Array with an Http Get
When Will a String Be Garbage Collected in Java
Sorting Java Objects Using Multiple Keys
Jcombobox Selection Change Listener
How to Select a Dropdown Value in Selenium Webdriver Using Java
What Is the Purpose of Flush() in Java Streams
Java - Get a List of All Classes Loaded in the Jvm
Open Resource with Relative Path in Java
Class Has Been Compiled by a More Recent Version of the Java Environment
Difference Between Validate(), Revalidate() and Invalidate() in Swing Gui