admin管理员组文章数量:1431726
I have this string: £0,00
Which i want to replace with float 3.95 for instance. But i want to keep the £ and the ","
So result -> £3,95
How would i do it?
--
Added some details:
Will the currency symbol always be a £?
The currency symbol might be before and sometimes behind the numbers. ie 0,00 kr
Will the separator always be ,, or might it be . or even an arbitrary character?
The separator might be . sometimes.
Will there always be two decimal places?
The decimal will always be 2 places.
How many integer digits might there be, and will there be a thousands separator?
It will not be above 100.
I have this string: £0,00
Which i want to replace with float 3.95 for instance. But i want to keep the £ and the ","
So result -> £3,95
How would i do it?
--
Added some details:
Will the currency symbol always be a £?
The currency symbol might be before and sometimes behind the numbers. ie 0,00 kr
Will the separator always be ,, or might it be . or even an arbitrary character?
The separator might be . sometimes.
Will there always be two decimal places?
The decimal will always be 2 places.
How many integer digits might there be, and will there be a thousands separator?
It will not be above 100.
-
You should give more detail on the bounds of your problem. Will the currency symbol always be a
£
? Will the separator always be,
, or might it be.
or even an arbitrary character? Will there always be two decimal places? How many integer digits might there be, and will there be a thousands separator? These are all important, since your original question can be answered trivially by ignoring the first and third characters, parsing the float and then adding them back in again. The range of valid input values is the essence of the question. – Andrzej Doyle Commented May 25, 2011 at 10:26 - Ok, good point. Will add more details – heffaklump Commented May 25, 2011 at 10:27
- How are you storing the number you wish to replace? is it var replace = 3.95 ? or? – AlanFoster Commented May 25, 2011 at 10:33
- Have a look at this question: stackoverflow./questions/149055/… – Ben Commented May 25, 2011 at 10:36
3 Answers
Reset to default 1function convert (proto, value) {
return proto.replace (/0(.)00/, function (m, dp) {
return value.toFixed (2).replace ('.', dp);
});
}
the proto parameter specifies the format, it must have a sub-string consisting of a single 0 digit followed by any character followed by two 0 digits, this entire sub-string is replaced by the numeric value parameter replacing the decimal point with the character between the 0 digits in the proto.
<script type="text/javascript">
function Convert(Value) {
return '£' + Value.toString().replace('.', ',');
}
alert(Convert(3.95));
</script>
Regular expression /(\D*)\s?([\d|.|,]+)\s?(\D*)/
will return an array with following values:
- [0] = whole string (eg "£4.30"
- [1] = prefix (eg. "£")
- [2] = numerical value (eg. "4.30")
- [3] = suffix (eg. "kr")
Usage: var parts = /(\D*)\s?([\d|.|,]+)\s?(\D*)/.exec(myValue);
It also handles cases where either prefix or suffix has following or leading space.
And whenever you need to replace the ma from number, just use the value.replace(",",".") method.
本文标签: regexReplace numbers in JavascriptStack Overflow
版权声明:本文标题:regex - Replace numbers in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745563986a2663647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论