简体   繁体   English

如何在JavaScript中解决Currency IDR

[英]How Resolve Currency IDR in javascript

How can resolve script like this ? 如何解决这样的脚本? for example count or minus Variable A and Variable B in currency IDR thanks, anyone can help me ... 例如,以货币IDR计算或减去变量A和变量B谢谢,任何人都可以帮助我...

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
<form name="form1">
<input id="harga" onkeyup="formatangka_titik()" type="text" />
<input id="diskon" onkeyup="formatangka_titik()" type="text" />
<input id="bayar" onkeyup="formatangka_titik()" type="text" />
</form>
</body>
</html>

here code javascript function : 这里的代码javascript函数:

<script type="text/javascript">
function formatangka_titik()
{
    a = form1.harga.value;
    b = a.replace(/[^\d]/g,"");
    c = "";
    panjang = b.length;
    j = 0;
    for (i = panjang; i > 0; i--)
    {
    j = j + 1;
    if (((j % 3) == 1) && (j != 1))
      {
    c = b.substr(i-1,1) + "." + c;
    } else {
    c = b.substr(i-1,1) + c;
    }
    }
    form1.harga.value = c;
</script>

I think your problem is that you are adding two strings together, which will concatenate the strings instead of add the numeric values. 我认为您的问题是,您要将两个字符串加在一起,这将串联字符串而不是添加数值。

Example adding string types: 添加字符串类型的示例:

var a = '1';
var b = '2';
console.log(a + b); // prints '12' to the console

Example adding int types: 添加int类型的示例:

var a = 1;
var b = 2;
console.log(a + b); // prints '3' to the console

JavaScript is loosely typed, so it's not always immediately apparent what a variable's type is. JavaScript是松散类型的,因此并不总是立即可以看出变量的类型是什么。

You can do a few things to change a string-type variable to an int. 您可以做一些事情来将字符串型变量更改为int。 Here are a couple common ways: 以下是几种常见的方法:

var stringNum = '123';
var intNum1 = parseInt(stringNum, 10);
var intNum2 = +stringNum;

Specifically, your code would need to look something like this: 具体来说,您的代码需要看起来像这样:

 function formatangka_titik() { var a = form1.harga.value.replace(/[^\\d]/g, ""); var b = form1.diskon.value.replace(/[^\\d]/g, ""); var a = +a; // converts 'a' from a string to an int var b = +b; // converts 'b' from a string to an int form1.harga.value = formatNum(a); form1.diskon.value = formatNum(b); form1.bayar.value = formatNum(+a + b); } function formatNum(rawNum) { rawNum = "" + rawNum; // converts the given number back to a string var retNum = ""; var j = 0; for (var i = rawNum.length; i > 0; i--) { j++; if (((j % 3) == 1) && (j != 1)) retNum = rawNum.substr(i - 1, 1) + "." + retNum; else retNum = rawNum.substr(i - 1, 1) + retNum; } return retNum; } 
 <form name="form1"> <input id="harga" onkeyup="formatangka_titik()" type="text" /> <input id="diskon" onkeyup="formatangka_titik()" type="text" /> <input id="bayar" onkeyup="formatangka_titik()" type="text" /> </form> 

在上面的示例中,我们如何计算IDR货币的百分比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM