how to append two strings in javascript without space? - javascript

I have two strings one brings me a time ie:
var gettime= $("#select-choice-2 :selected").text();
it gives me time in 24 hr format like this
17:45
but i want my time to be in a format like
17:45:00.000
for which i made a string
var ext=':00.000';
I want these two strings to concatenate in such a way to give me proper result.
I see now whats the problem is my "gettime" is not a proper string, i tried it to show in alertbox but nothing happens, so please tell me how to convert gettime into a string.
I got it "gettime" is a local variable and ext is using in some other function thats why "gettime" was not appearing in alertbox, stupid ehh :p

Simply use the concatenation operator:
alert( gettime + ext );

Do you just wand to add the strings together?
In that case:
var bothstrings = gettime + ext;

some browsers can cause line-breaks to the resulting string if you directly assign a + between strings to joing them. The standard way to do is -
gettime.concat(ext)

**
scope problem,i get it now. gettime was a local variable within some function and ext is held in different function thats why in the function gettime is not appearing in alertbox **

Related

How to convert date format coming in an ajax object?

I am getting 2018-06-10 00:29:04 this type of value in the key date. I just want to display date without time.
I want to have 2018-06-10 from 2018-06-10 00:29:04.
If it's a fixed string that you're working with, as in you know it'll always be in that format, you can just truncate it like so:
var datetimestamp = "2018-06-10 00:29:04";
var dateTruncated = datetimestamp.slice(0, 10);
If it's not fixed, you can split on spaces like #Shubh mentioned in his comment and take the first array value.

How to get an 8 decimal output?

I am trying to get an 8 decimal output from the following function.
The following function multiplies an input by 2 and then updates this input with the wagerUpdate variable. I would like this outputted number to have 8 decimal places.
For example: if input number is 0.00000001 (this code is for a bitcoin website), then I would like output number to be 0.00000002. For some reason the code below is not working properly as the output number is in the format of 2e-8 without the .toFixed(8) code. Please help if you are able to. Thank you so much.
<script>
function MultiplyWagerFunction() {
var wager = document.getElementById("wagerInputBox").value;
var wagerUpdate = wager*2;
document.getElementById("wagerInputBox").value = +wagerUpdate.toFixed(8);
}
</script>
If you remove the + before wagerUpdate.toFixed(8) it should work fine. wagerUpdate has already be converted to a number when you multiplied it by 2 so there should be no need for the unary +
var a = "0.00000001";
var b = a*2;
console.log(b.toFixed(8));
console.log(+b.toFixed(8));
^ see the difference.
The reason it doesn't work is because what you are doing is equivalent to:
+(b.toFixed(8))
because of the precedence of the operators (member access . is higher than unary +). You are converting b to a string with .toFixed and then converting it back into a number with + and then converting it back into a string again! (this time with the default toString behavior for numbers giving you exponential notation)
Just remove + from +wagerUpdate.toFixed(8); and you would be good.
Instead of:
document.getElementById("wagerInputBox").value = +wagerUpdate.toFixed(8);
try:
document.getElementById("wagerInputBox").innerHTML = +wagerUpdate.toFixed(8);
Why I say so is may be when you set value, browser tries to convert to best possible outcome. But, inner HTML should take the string equivalent!

How to "unformat" a numerical string? JavaScript

So I know how to format a string or integer like 2000 to 2K, but how do I reverse it?
I want to do something like:
var string = "$2K".replace("/* K with 000 and remove $ symbol in front of 2 */");
How do I start? I am not very good regular expressions, but I have been taking some more time out to learn them. If you can help, I certainly appreciate it. Is it possible to do the same thing for M for millions (adding 000000 at the end) or B for billions (adding 000000000 at the end)?
var string = "$2K".replace(/\$(\d+)K/, "$1000");
will give output as
2000
I'm going to take a different approach to this, as the best way to do this is to change your app to not lose the original numeric information. I recognize that this isn't always possible (for example, if you're scraping formatted values...), but it could be useful way to think about it for other users with similar question.
Instead of just storing the numeric values or the display values (and then trying to convert back to the numeric values later on), try to update your app to store both in the same object:
var value = {numeric: 2000, display: '2K'}
console.log(value.numeric); // 2000
console.log(value.display); // 2K
The example here is a bit simplified, but if you pass around your values like this, you don't need to convert back in the first place. It also allows you to have your formatted values change based on locale, currency, or rounding, and you don't lose the precision of your original values.

Trouble converting javascript string to number

I have a script assigns a variable using parseFloat as follows:
var vendorCost = parseFloat(vendorSearchresults[0].getValue('vendorcost')).toFixed(2);
I assumed this would make the variable a number. However when I check it with typeof - it reports it as a string. My solution is as follows:
vendorCost = parseFloat(vendorCost);
Which works, however I'm trying to be more efficient when coding and would like to understand why it doesn't make vendorCost a number when assigning it a number? Is there a way I could make the first statement make vendorCost a number without the need for the second statement? Thanks in advance.
Update - just thought I should mention I'm having the exact same issue without using .toFixed -
var vendorLandedCost = parseFloat(vendorSearchresults[0].getValue('custentity_asg_landed_cost','vendor'));
vendorLandedCost = parseFloat(vendorLandedCost);
The last toFixed() call converts the result of the first parseFloat into a string.
Looks like you need to round the number to two decimal places, which is why you're using the parseFloat call. You can do something like this instead:
vendor_cost = Math.round(parseFloat(vendorSearchresults[0].getValue('vendorcost')) * 100) / 100
Well, Number.toFixed returns string because it is a data presentation function.
See the docs.

Convert string, format 10:30AM, to which type for comparison in javascript

I have strings of the format "10:30AM", "3:00PM" etc that I want to be able to use basic operations on, for example > or < and how many hours until say 10:30 based on current time. I would like to make the conversion on the client side (javascript/jQuery) prior to database insertion.
Should I convert these to javascript date-time objects? or would a regex to change it to say a number in 24hour time format be more suitable to perform these operations on? Or am I making this more difficult than it should be?
Thanks in advance.
You are going to want to convert to a date time -- there are a lot of edge cases when comparing numbers as strings -- much easier to just bite the bullet and make a date out of it. There are a million example libraries to use or take inspiration from.
I personally think if it is basic operations i would convert it to 24h and then compare. If it was anything more complex then I would convert it to a date-time object.
I would suggest you to use a library for that. I prefer Moment.js which allows you to perform compare or know how many hours from the current time.
It's a bit late but when you're sure you have such a specific string that needs converting in a specific way you could write your own implementation to convert the time, it'll be lighter and quicker to sort or compare:
var Time=function(time){
// TODO: you an check here what format the time variable
// is and if it's possible to convert it to time or milTime
this.time=time;
this.milTime=this.toMilTime();
this.val=this.setVal();
};
Time.prototype.toMilTime=function(){
return this.time.replace(/([0-9]{1,2}).([0-9]{1,2})([\w]{2})/,function(){
//TODO: put this in a try catch and check if hours and numbers
// are correct numbers. throw a new Error with the correct description
var hours=(arguments[1].length===1)?"0"+arguments[1]:
arguments[1],
minutes=(arguments[2].length===1)?"0"+arguments[2]:
arguments[2],
pam=arguments[3].toUpperCase();
if(pam==="PM"){
hours=parseInt(hours,10)+12;
}
return hours + ":" + minutes;
});
};
Time.prototype.setVal=function(){
return parseInt(this.milTime.replace(/:/,""),10);
}
// used for sorting
Time.prototype.valueOf=function(){
return this.val;
};
// needed for <> comparison
Time.prototype.toString=function(){
return this.milTime;
};
var t = new Time("10:30AM"),
t1=new Time("1:00PM"),
t2=new Time("10:30AM");
console.log(t.milTime,t1.milTime);
console.log(t>t1);// this will use toString()
console.log(t1>t);// this will use toString()
console.log(t===t2);//false
console.log(t==t2);//false
console.log(t.milTime===t2.milTime);//true
var arr=[t,t1,t2].sort(function(a,b){
return a.valueOf()-b.valueOf();// force it to use valueOf
});
console.log(arr);

Categories

Resources