我需要100000的印度货币格式为1,00,000,1234为1,234

时间:2015-12-09 07:55:05

标签: javascript java currency

我需要印度货币格式如100000为1,00,000,1234为1,234。

我试过这段代码,

function currencyFormat1(id) {
    var x;
    x = id.toString();
    var lastThree = x.substring(x.length - 3);
    var otherNumbers = x.substring(0, x.length - 3);
    if (otherNumbers != '')
        lastThree = ',' + lastThree;
    var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
    return  res;
}

但它只在java脚本中工作, 我需要这个作为核心java代码,我试图将其转换为

var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;

8 个答案:

答案 0 :(得分:1)

在Java中,正则表达式语法略有不同。 /\B(?=(\d{2})+(?!\d))/g,需要成为"?g\\B(?=(\\d{2})+(?!\\d))"

答案 1 :(得分:1)

java语法大致如下。

请注意,我在他/她的回答中使用了 npinti 给出的正则表达式:

public String currencyFormat1(Object id) {
                            String x;
                            x = id.toString();
                            String lastThree = x.substring(x.length() - 3);
                            String otherNumbers = x.substring(0, x.length() - 3);
                            if (!otherNumbers.isEmpty())
                                lastThree = "," + lastThree;
                            String res = otherNumbers.replaceAll("?g\\B(?=(\\d{2})+(?!\\d))", ",") + lastThree;
                            return  res;
                        }

答案 2 :(得分:1)

Java NumberFormat会给你你想要的东西,但你也可以编写自己的方法:

public static String fmt(String s){

    String formatted = "";
    if(s.length() > 1){
        formatted = s.substring(0,1);
        s = s.substring(1);
    }

    while(s.length() > 3){
        formatted += "," + s.substring(0,2);
        s = s.substring(2);
    }
    return formatted + "," + s + ".00"; 
}

<强>测试

System.out.println(fmt("1234"));
System.out.println(fmt("100000"));
System.out.println(fmt("12345678"));
System.out.println(fmt("123456789"));                       
System.out.println(fmt("1234567898"));

<强>输出:

1,234.00
1,00,000.00
1,23,45,678.00
1,23,45,67,89.00
1,23,45,67,898.00

答案 3 :(得分:0)

印度货币编号转换正则表达式

**/(\d+?)(?=(\d\d)+(\d)(?!\d))(.\d+)?/g, "$1,"**

$(this).val(parseFloat($(this).val(), 10).toFixed(2).replace(/(\d+?)(?=(\d\d)+(\d)(?!\d))(.\d+)?/g, "$1,").toString());

外币号码转换正则表达式

**/(\d)(?=(\d{3})+.)/g, "$1,"**

$(this).val(parseFloat($(this).val(), 10).toFixed(2).replace(/(\d)(?=(\d{3})+.)/g, "$1,").toString());

答案 4 :(得分:0)

如果一个特定语言环境仅需要一种特定样式,则简单的手动解决方案可能会起作用(有关这种解决方案,请参见其他答案)。如果没有...

对于Javascript,请检出Globalize

安装

npm install globalize cldr-data --save

然后: (此印地语的特殊示例从先前的答案here中删除-native,使其仅根据区域设置插入逗号)

var cldr = require("cldr-data");
var Globalize = require("globalize");

Globalize.load(cldr("supplemental/likelySubtags"));
Globalize.load(cldr("supplemental/numberingSystems"));
Globalize.load(cldr("supplemental/currencyData"));

//replace 'hi' with appropriate language tag
Globalize.load(cldr("main/hi/numbers"));
Globalize.load(cldr("main/hi/currencies"));

//You may replace the above locale-specific loads with the following line,
// which will load every type of CLDR language data for every available locale
// and may consume several hundred megs of memory!
//Use with caution.
//Globalize.load(cldr.all());

//Set the locale
//We use the extention u-nu-native to indicate that Devanagari and
// not Latin numerals should be used.
// '-u' means extension
// '-nu' means number
// '-native' means use native script
//Without -u-nu-native this example will not work
//See 
// https://en.wikipedia.org/wiki/IETF_language_tag#Extension_U_.28Unicode_Locale.29
// for more details on the U language code extension 
var hindiGlobalizer = Globalize('hi-IN-u-nu-native');

var parseHindiNumber = hindiGlobalizer.numberParser();
var formatHindiNumber = hindiGlobalizer.numberFormatter();
var formatRupeeCurrency = hindiGlobalizer.currencyFormatter("INR");

console.log(parseHindiNumber('३,५००')); //3500
console.log(formatHindiNumber(3500));   //३,५००
console.log(formatRupeeCurrency(3500)); //₹३,५००.००

https://github.com/codebling/globalize-example

答案 5 :(得分:-1)

user3437460提供的代码不适用于所有可能的情况(EX:123456)。以下代码适用于所有情况。

import java.util.Scanner;

public class INRFormat {

    public static String format(String num){
        System.out.println(num.length());
        String temp ="";
        //Removing extra '0's 
        while(num.substring(0,1).equals("0") && num.length()>1) {
            num = num.substring(1);
        }
        //Returning if it is 3 or less than 3 digits number.
        if(num.length()<=3){
            return num+".00";

        }
        //If it is an odd number then ',' will start after first digit(Ex: 1,23,456.00)
        else if(num.length()%2 !=0) {
            temp = num.substring(0, 2);
            num = num.substring(2);}
        //If it is an even number then ',' will start after first 2 digits(Ex: 12,34,567.00)
        else {
            temp = num.substring(0, 1);
            num = num.substring(1);
        }
        while (num.length() > 3) {
                temp += "," + num.substring(0, 2);
                num = num.substring(2);

            }

        return temp+","+num+".00";
    }
    public static void main(String args []){
         Scanner s =new Scanner(System.in);
         System.out.println("Please enter the number to be formatted:" );
         String num =s.nextLine();
         System.out.println("Formatted number:" + format(num));

    }

}

答案 6 :(得分:-2)

在Java中,您也可以使用DecimalFormat:

double val = Double.parseDouble(num);  //If your num is in String  
DecimalFormat fmt = new DecimalFormat("#,###.00");
System.out.println(fmt.format(val));

答案 7 :(得分:-2)

&#13;
&#13;
$('#textfield').keyup(function() {	
var value=$('#textfield').val();
      
      var newvalue=value.replace(/,/g, '');   
      var valuewithcomma=Number(newvalue).toLocaleString('en-IN');   
	 
      $('#textfield').val(valuewithcomma); 

      });
		
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<form><input type="text" id="textfield" ></form>
&#13;
&#13;
&#13;