/    Sign up×
Community /Pin to ProfileBookmark

How can I add dot after first character if number >= 1000 ?

[CODE]
function addDot(a) {
if (a >= 1000) {
// I don’t know how to do
}
}
[/CODE]

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@cootheadMar 09.2018 — [indent]

Hi there mikefromru,

[indent]

try it like this....

[color=navy]
if ( a >= 1000 ) {
a = a + '.';
}[/color]


Be aware though that [i]"a"[/i] is no longer a number.

[/indent]


[i]coothead[/i]


[/indent]
Copy linkTweet thisAlerts:
@mikefromruauthorMar 09.2018 — Thank you!
Copy linkTweet thisAlerts:
@JMRKERMar 09.2018 — Thank you![/QUOTE]

I read your request in post #1 as to put the '.' after the FIRST character, not the last.

To me that would be 1.000 instead of 1000.

If 'coothead's answer was good enough for you, then OK.
Copy linkTweet thisAlerts:
@mikefromruauthorMar 09.2018 — I read your request in post #1 as to put the '.' after the FIRST character, not the last.

To me that would be 1.000 instead of 1000.

If 'coothead's answer was good enough for you, then OK.[/QUOTE]

No, his answer isn't right for me. I don't understen why he answered so. You're right. Actually I already did it.

function setDoc(a) {

if (a >= 1000) {

var d = 1000

var f = parseFloat(a)

console.log(f/d)

}else{

console.log(a)

}

}
Copy linkTweet thisAlerts:
@cootheadMar 09.2018 — ..............
Copy linkTweet thisAlerts:
@mikefromruauthorMar 09.2018 — Sorry:

function addDot(a) {

a = a.toString();

return + a >= 1000? a.charAt(0) + '.' + a.substr(1) : a;

}
Copy linkTweet thisAlerts:
@JMRKERMar 10.2018 — Could also use:
<i>
</i>var a = 1000,
b = a.toLocaleString("en").replace('.',','); alert(b);


or
<i>
</i>var a = (1000).toLocaleString("en").replace('.',','); alert(a);
var b = (9999).toLocaleString("en").replace('.',','); alert(b);
var c = (19999).toLocaleString("en").replace('.',','); alert(c);
var d = (500).toLocaleString("en").replace('.',','); alert(d);

Copy linkTweet thisAlerts:
@rootMar 10.2018 — This was asked in another thread the other day and I posted a nice prototype REGEX option for formatting a string with a comma, you just need to swap the , for a . and f you good to go for numbers like 1, 1.00, 100.000 and so on.

Number.prototype.numberFormat = function(){
return this.toString().replace( /(d)(?=(d{3})+(?!d))/g, "$1," );
};

Use: [B]var myResult = myNumber.numberFormat();[/B] to get the number formatted as a number with commas.

This should work Number.prototype.numberDotFormat = function(){
return this.toString().replace( /(d)(?=(d{3})+(?!d))/g, "$1." );
};

Use: [B]var myResult = myNumber.numberDotFormat();[/B] to get the format you want.
Copy linkTweet thisAlerts:
@wbportMar 10.2018 — I think he would want to keep the original number intact (so it could be manipulated further) and keep another "number" (really character string) to display. Some locales use "." for a decimal point and "," to separate groups of three digits in front of it, for some it is vice versa. Perhaps put the code to do this in a function so what the function returns could be displayed or printed.

[B]Never mind, I see this was implemented in the previous post.[/B]
Copy linkTweet thisAlerts:
@rootMar 11.2018 — A string number is easily turned in to a numeric again with parseInt or parseFloat.
×

Success!

Help @mikefromru spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...