formatting double value for pricing data in JS - javascript

I have a pricing table, I want to convert the data as shown in below.
Is there a specific way to do it other than doing mod and having a if condition
Original input = Output I needed
123.0 = 123
22.2 = 22.2
22.21 = 22.21
This is for javascript not Java.

Just do a toString() to get rid of trailling zeroes after decimal like this
var n = 123.0;
n = parseFloat(n.toString()) //outputs 123
make a method out of it
function removeTraillingZeroes( n ){ return parseFloat(n.toString()) }
removeTraillingZeroes( 123.3430 ); //outputs 123.343
removeTraillingZeroes( 123.330 ); //outputs 123.33
Note - You will need to assign the value back to n, if you want to use the same variable.
var n = 123.0;
n = removeTraillingZeroes( n ); //outputs 123
console.log(n);

Just return the values!
var array = [123.0, 22.2, 22.21];
document.write(array.join('<br>'));

Related

What am I missing doing bitwise operations in javascript?

Trying to bitwise OR a group of values and wrote a some test code that doesn't return what I would expect ( old C programmer ). My test code:
// take 3 values from string ( 1,2,3 ) and OR them together
var values="012345678"; // sample characters
var val=0; // int to place single ascii value
var bin=0; // binary value after offset
var total=0; // cumulative total
var pos=1; //where to start pulling characters
// take 3 values from string ( 1,2,3 ) and OR them together
for(let i=0;i<3;i++){
var singleVal=values[pos++];
val=Number(singleVal.charCodeAt(0));
bin=val-48; // position offset by ascii "0" = 48
total|=bin;
}
// Result should be 7 but always returns the last singleVal
console.log("total: "+total);
Result should be 7 but always returns the last singleVal
with bitwise or the result of 1|2|3 is 3, not 7 (01|10|11 = 11).
If you loop until 7 then the result would be 7:
for(let i=0;i<7;i++){

Array Remove last 2 digits of a number

I would like to create a program that takes a number is input, such as: 12345 and then splits this number into 2 digit numbers and store it in a array. The array must look like this: [0]=45 [1]=23 [2]=1 . This means that the splitting of the numbers must start from the last digit of the number and not the first.
This is what I have until now:
var splitCount = []; // This is the array in which we store our split numbers
//Getting api results via jQuery's GET request
$.get("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCJwchuXd_UWNxW-Z1Cg-liw&key=AIzaSyDUzfsMaYjn7dnGXy9ZEtQB_CuHyii4poc", function(result) {
//result is our api answer and contains the recieved data
//now we put the subscriber count into another variable (count); this is just for clarity
count = result.items[0].statistics.subscriberCount;
//While the subscriber count still has characters
while (count.length) {
splitCount.push(count.substr(0, 2)); //Push first two characters into the splitCount array from line 1
count = count.substr(2); //Remove first two characters from the count string
}
console.log(splitCount) //Output our splitCount array
});
but the problem with this is that if there are 5 digits for example: 12345 the the last digit will be in an array by itself like this: [0]=12 [1]=34 [2]=5 but I need the last array to have 2 digits and the first should be the one with one digit instead like this: [0]=1 [1]=23 [2]=45
very crude but this should work assuming the string is always numbers:
input = "12345"
def chop_it_up(input)
o = []
while input.length > 0
if input.length <= 2
o << input
else
o << input[-2..input.length]
end
input = input[0..-3]
chop_it_up(input)
end
return o
end
I probably do sth like this :
int[] fun(int x){
int xtmp = x;
int i = 0;
int len = String.valueOf(x).length();
// this is a function for java, but you can probably find
//an equivalent in whatever language you use
int tab[(len+1)/2];
while(xtmp > 1){
tab[i] = xtmp%100;
xtmp = int(xtmp/100); // here you take the integer part of your xtmp
++i;
}
return tab;
}

How to hide numbers after dot in float value in input using jquery

I am developing a financial calculator and i am facing a problem to get exact answers if i round the value . Basically i dont want to show numbers after dot in float value, but i need the same float value in my calculations.
Note :- There are too many calculations so it is tough to save values in variables
for example
value1 = Math.round(34.55); // it will be 35
$('#input1').val(value1); // and it is fine to show round value which is 35
value2 = ("#input1").val(); // In this line it will be 35 but i need 34.55
try this
var floatVal = 34.55;
value1 = Math.round(floatVal); // it will be 35
$('#input1').attr("data-floatVal","34.55");
$('#input1').val(value1);
//it's important to parseFloat
value2 = parseFloat($("#input1).attr("data-floatVal"));
set it as an attribute of that input element
var decimalValue = 34.55;
$('#input1').attr( "data-value", decimalValue );
$('#input1').val( Math.round( decimalValue ) );
value2 = ("#input1).val(); //decimal value
actualValue2 = ("#input1).attr( "data-value" ); // float value
try this //I hope this will work for you.
roundValue = Math.round(34.55);
actualValue=34.55;//Create another variable to store actual value and use when required.
$('#input1').val(roundValue);
value2 = actualValue;
try this //I hope this will work for you.
roundValue = Math.round(34.55);
actualValue=34.55;//Create another variable to store actual value and use when required.
$('#input1').val(roundValue);
value2 = actualValue;

get wrong result function jquery javascript

I am using the following script. But I am receiving a wrong result for x_b_bbetrag.
When do an calculation exp 100/108 I get 9.92 instead of 92.59.
What am I missing here?
Code below:
var betrag = 100
var kurs = 1
var minkl= 1
var msatz= 0.08
$("#x_b_betrag").change(function() {
var betrag = $("#x_b_betrag").val();
var kurs = $("#x_b_kurs").val();
var minkl =$("input[name='x_b_mwstinkl']:checked").val();
var msatz =$("input[name='x_b_mwst']:checked").val();
if (minkl == "1"){
$("#x_b_rechenbetrag").val((betrag * kurs).toFixed(2));
$("#x_b_bbetrag").val( ( (betrag * kurs) /(1 + msatz) ).toFixed(2));
}
Use parseFloat
multiplication, division and subtraction automatically parse string to number. for summation you need to parse it.
$("#x_b_bbetrag").val( ( (betrag * kurs) /(1 + parseFloat(msatz) ) ).toFixed(2));
///1 + "1" = 11 not 2
Parse your inputs into numbers.
For example :
var betrag = parseFloat($("#x_b_betrag").val());
MDN on parseFloat
The value of the msatz variable is not 0.08 but "0.08". It's a string, so when you add one to it, the number will be converted to a string so that they can be concatenated, and the result is "10.08" not 1.08. The string will implicitly be converted to a number when you use it in the division, as it's not possible to divide by a string.
Parse the string into a number:
var msatz = parseFloat($("input[name='x_b_mwst']:checked").val());

Removing an integer from an id

I have an id:
div id ="untitled-region-5"
I want to grab that id and remove 4 from it and then do some code with the new id.
So far I am trying something like this from reading about how to perform this:
var n = $(this).attr('id').match(/untitled-region-(\d+)/)[1];
But I don't know how to remove 4 from the integer.
It's still a string, that's why you can't easily use it in math. Some operations work, because they will implicitly convert the value to a number.
Use parseInt to explicitly do the conversion, so that you know what you have:
var n = parseInt($(this).attr('id').match(/\d+$/)[0]);
n -= 4;
var id = $("div").attr("id");
var n = parseInt(id.substring(id.lastIndexOf("-") + 1), 10);
I made a jsFiddle too: http://jsfiddle.net/YEWQQ/
<div id ="untitled-region-5">​
just take n and subtract 4
var n = $('div').attr('id').match(/untitled-region-(\d+)/)[1];
var newNumber = n-4;
$('div').attr('id','untitled-region-'+newNumber);

Categories

Resources