number manipulation in angular - html

I was trying to convert some number automatically from x=1 to x=00001 so that x takes 5 spaces in total. If x=20.2, I want to format x to be 00020. How can I do that?
I am using html and angularjs. Thanks.

Is this what you want?
function convert(input)
{
var a = Math.floor(input).toString();
return ("00000").substr(0, 5 - a.length) + a;
}
Example:
console.log(convert(20.2)); // 00020

write this filter to achieve the logic
<div>{{number | decimalConvertion:5}}</div>
filter('decimalConvertion', function() {
return function(str, max){
function pad(str, max){
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
return pad(str,8);
};
}
for your reference jsfiddle link

Related

Automatically add comma for monetary values in a form?

Not sure if I described my question well, but basically here's what I've got right now:
$('input.number').keyup(function(event) {
// skip for arrow keys
if(event.which >= 37 && event.which <= 40) return;
// format number
$(this).val(function(index, value) {
return value
.replace(/\D/g, "")
.replace(/\B(?=(\d{3})+(?!\d))/g, ",")
;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input class="number">
Right now if you enter 1000 it will add comma like this: 1,000
What I want is the number to act as a cent.
So if I write 100 it will add a dot here: 1.00
If 1000, then 10.00
If 10000 then 100.00
If 100000 then 1,000.00
and so on.
basically I want the number to be a cent and add commas and dots with a jQuery accordingly.
But I don't want them to be submitted.
I have seen this being done in ad networks, kubikads for example.
The numbers should be submitted without commas and dots.
The jQuery code in the above code seems very confusing to me .
If anyone have a ready made script or know what to modify in the script to achieve this, I would greatly appreciate
A little dirty... but it works! You can just pop off the decimal and store it while you add the commas.
$('input.number').keyup(function(event) {
// skip for arrow keys
if(event.which >= 37 && event.which <= 40) return;
// format number
$(this).val(function(index, value) {
return value
.replace(/\D/g, "")
.replace(/^0+/,"")
.split(/(\d{0,2})$/)
.join(".")
.replace(/\B(?=(\d{3})+(?!\d))/g, ",")
.replace(/.$/,"")
});
});
$('#myform').submit(function(e) {
e.currentTarget[0].value = e.currentTarget[0].value
.replace(/\D/g, "")
console.log(e.currentTarget[0].value)
return false; // return false to cancel form action
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<form id="myform">
<input class="number">
</form>
This should do it...
var num = '132406'; /Your original unformatted number
var ret = '';
var p = 0;
for (let i = num.length; i > 0; i--) {
p = p + 1;
if (p == 3 && ret.includes('.') == false) {
ret = '.' + ret;
p = 0;
} else if (p % 3 == 0 ) {
ret = ',' + ret;
}
ret = num.substring(i - 1, i) + ret;
}
console.log(ret);

Is there way to modify Number class

At clasic windows calculator, nubers are separated by comma, like: 4,5
I wish to have it same in flash, but:
Number("4,5") //output: NaN
Is there any way modify Number class to get this result?
Number("4,5") //output: 4.5
Number("4.5") //output: 4.5
Also string with spaces can be considered as number...
trace(Number("10000" )) //output: 10000
trace(Number("10 000")) //output: NaN
one more example about numbers which can be fixed:
trace(4.3 - 1.1) //output 3.1999999999999997
trace(Number(4.3 - 1.1).toFixed(1)) //output: 3.2
Instead of using Number , use a function and send the values to it , just like this
function convertNumber(var:String):Number
{
var arr:Array;
arr = var.split(",");
if(arr.length >0)
{
var sendStr:String = arr[0] + "." + arr[1];
return Number(sendStr);
}
arr = var.split(" ");
if(arr.length >0)
{
var sendStr:String = arr[0] + arr[1];
return Number(sendStr);
}
return Number(var)
}
use convertNumber(4,5) to get output as 4.5

I have a function that changes months into numbers, but I'd like to add 0 before the month's number

I have a date like:
19/août/2016 (août = august)
And I have the following function which changes the month into a number:
function swapMonthForNumber(str:String):String {
//do the same line of code for every item in the array
for(var i:int=0;i<months.length;i++){
//i is the item, which is 0 based, so we have to add 1 to make the right month number
str = str.replace(months[i],String(+i+1));
}
//return the updated string
return str;
}
str = swapMonthForNumber(mySharedObject.data.theDate);
trace("Php will use this date :"+str);
So str will be 19/8/2016, but I want str to be 19/08/2016 (adding a 0 before the 8).
How can I do this?
Check out the reference of the Date class!
If forgot to mention this link : flash.globalization.DateTimeFormatter
DateTimeFormatter(requestedLocaleIDName:String, dateStyle:String = "long", timeStyle:String = "long")
Here is an example.
import flash.globalization.DateTimeFormatter;
var df:DateTimeFormatter = new DateTimeFormatter(LocaleID.DEFAULT, DateTimeStyle.SHORT, DateTimeStyle.NONE);
var currentDate:Date = new Date(2016,7,19);
var shortDate:String = df.format(currentDate);
trace (shortDate);
// output : 19/08/2016
DateTimeStyle
LocaleID
Adding leading zeros to a number is commonly called zero padding.
Below is a function to do this, from the answer here.
public function zeroPad(number:int, width:int):String {
var ret:String = ""+number;
while( ret.length < width )
ret="0" + ret;
return ret;
}
In your swapMonthForNumber function, in the for loop, swap the code for this:
var month = zeroPad(i + 1, 2);
str = str.replace(months[i], month);

Customize .y([yScale]) method in dc.js

I'm trying to create a Bar chart in DC.js. Here I've reduced avg of a field and want to display that on the y-axis. I have no clue on how to do it. Can somebody help me on this?
var dimByChannel = cf.dimension(function(d) { return d.channelUUID; });
var groupByChannel = dimByChannel.group().reduce(reduceAdd, reduceRemove, reduceInitial);
function reduceAdd(p, v) {
p.bytesTxd = p.bytesTxd + v.bytesTxd;
p.avg = (p.bytesTxd/total)*100;
p.count = p.count + 1;
return p;
}
function reduceRemove(p, v) {
p.bytesTxd = p.bytesTxd - v.bytesTxd;
p.avg = (p.bytesTxd/total)*100;
p.count = p.count - 1;
return p;
}
function reduceInitial() {
return {
bytesTxd: 0,
avg: 0,
count: 0
};
}
I've calculated total using reduceSum(). Till now everything is good. Now how to show the reduced avg on y-axis?
chanUtil
.dimension(dimByChannel)
.group(groupByChannel)
.x(d3.scale.ordinal().domain(data.map(function (d) { return d.channelUUID; })))
.xUnits(dc.units.ordinal)
//.y(d3.scale.linear().domain(data.map(function (d) { return d.avg; })))
.yAxisLabel("Utilization %");
Yes, .y() is rather deceptively (or vaguely) named: that one is for the scale, which maps chart coordinates to physical coordinates.
What you want here is .valueAccessor():
.valueAccessor(function(d) { return d.value.avg; })

How can I make my Array to return a randomly value?

How can I make my Array to return a randomly value?
I want AirUnit to return a value between 1 and 3.
I want LandUnit to return a value between 4 and 6.
I want WaterUnit to return a value between 7 and 9.
function getRandom():uint
{
var unitList:Array = ["AirUnit", "LandUnit", "WaterUnit"];
for (var i:int = 0; i < unitList.length; i++)
{
// Display the elements in the Output panel.
var random_no:Number = Math.round(Math.random()*2);
//trace("Element " + unitList.indexOf(unitList[random_no])+ ": " + unitList[random_no]);
}
return random_no;
}
trace(getRandom());
If you wanna get random values only use the below one.
function getRandom():uint
{
return Math.round(Math.random()*2);
}
trace(getRandom());