Angular 2 money number space after 3 zeros












-1














for example I have number : 30000 and I want to display it like this : 30 000. What should I use for it ?



More examples : 300000 -> 300 000,



3000000 -> 3000 000.



And not it's not about dots or comas, I want to recognize how many numbers there are and put space between numbers.










share|improve this question
























  • Possible duplicate of How to format numbers in JavaScript?
    – emix
    Nov 13 '18 at 8:39










  • nah its not, there about . and ,
    – Angulandy2
    Nov 13 '18 at 8:39










  • There are plenty of examples and solutions here on SO.
    – emix
    Nov 13 '18 at 8:41












  • If you knew you wouldnt comment that. No need of those non sence comments here.
    – Angulandy2
    Nov 13 '18 at 8:42










  • Possibly related: stackoverflow.com/questions/30843217/…
    – McVenco
    Nov 13 '18 at 8:43
















-1














for example I have number : 30000 and I want to display it like this : 30 000. What should I use for it ?



More examples : 300000 -> 300 000,



3000000 -> 3000 000.



And not it's not about dots or comas, I want to recognize how many numbers there are and put space between numbers.










share|improve this question
























  • Possible duplicate of How to format numbers in JavaScript?
    – emix
    Nov 13 '18 at 8:39










  • nah its not, there about . and ,
    – Angulandy2
    Nov 13 '18 at 8:39










  • There are plenty of examples and solutions here on SO.
    – emix
    Nov 13 '18 at 8:41












  • If you knew you wouldnt comment that. No need of those non sence comments here.
    – Angulandy2
    Nov 13 '18 at 8:42










  • Possibly related: stackoverflow.com/questions/30843217/…
    – McVenco
    Nov 13 '18 at 8:43














-1












-1








-1







for example I have number : 30000 and I want to display it like this : 30 000. What should I use for it ?



More examples : 300000 -> 300 000,



3000000 -> 3000 000.



And not it's not about dots or comas, I want to recognize how many numbers there are and put space between numbers.










share|improve this question















for example I have number : 30000 and I want to display it like this : 30 000. What should I use for it ?



More examples : 300000 -> 300 000,



3000000 -> 3000 000.



And not it's not about dots or comas, I want to recognize how many numbers there are and put space between numbers.







javascript angular typescript internationalization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 8:40







Angulandy2

















asked Nov 13 '18 at 8:37









Angulandy2Angulandy2

23612




23612












  • Possible duplicate of How to format numbers in JavaScript?
    – emix
    Nov 13 '18 at 8:39










  • nah its not, there about . and ,
    – Angulandy2
    Nov 13 '18 at 8:39










  • There are plenty of examples and solutions here on SO.
    – emix
    Nov 13 '18 at 8:41












  • If you knew you wouldnt comment that. No need of those non sence comments here.
    – Angulandy2
    Nov 13 '18 at 8:42










  • Possibly related: stackoverflow.com/questions/30843217/…
    – McVenco
    Nov 13 '18 at 8:43


















  • Possible duplicate of How to format numbers in JavaScript?
    – emix
    Nov 13 '18 at 8:39










  • nah its not, there about . and ,
    – Angulandy2
    Nov 13 '18 at 8:39










  • There are plenty of examples and solutions here on SO.
    – emix
    Nov 13 '18 at 8:41












  • If you knew you wouldnt comment that. No need of those non sence comments here.
    – Angulandy2
    Nov 13 '18 at 8:42










  • Possibly related: stackoverflow.com/questions/30843217/…
    – McVenco
    Nov 13 '18 at 8:43
















Possible duplicate of How to format numbers in JavaScript?
– emix
Nov 13 '18 at 8:39




Possible duplicate of How to format numbers in JavaScript?
– emix
Nov 13 '18 at 8:39












nah its not, there about . and ,
– Angulandy2
Nov 13 '18 at 8:39




nah its not, there about . and ,
– Angulandy2
Nov 13 '18 at 8:39












There are plenty of examples and solutions here on SO.
– emix
Nov 13 '18 at 8:41






There are plenty of examples and solutions here on SO.
– emix
Nov 13 '18 at 8:41














If you knew you wouldnt comment that. No need of those non sence comments here.
– Angulandy2
Nov 13 '18 at 8:42




If you knew you wouldnt comment that. No need of those non sence comments here.
– Angulandy2
Nov 13 '18 at 8:42












Possibly related: stackoverflow.com/questions/30843217/…
– McVenco
Nov 13 '18 at 8:43




Possibly related: stackoverflow.com/questions/30843217/…
– McVenco
Nov 13 '18 at 8:43












4 Answers
4






active

oldest

votes


















4














consider using a pipe that takes the number, convert it to a string and format it as you like.. you could add a currency two.



example:(more of a point illustration than a working code.. can't test at the moment)



//convert the string to an array
var arr = str.split("");
//back to string with added spaces
let string = "";
let count = 0;
for(i=arr.length-1; i>=0; i--){
if(count<3){
string = arr[i] + string;
count = count+1;
}
else{
string = " " + string;
count=0;
i=i+1;
}
}





share|improve this answer



















  • 1




    Can you provide an example please.
    – Jonathan Stellwag
    Nov 13 '18 at 8:41






  • 2




    @JonathanStellwag example is not necessary, because no code was given. The question was what to use for it. And a pipe is the best solution
    – PierreDuc
    Nov 13 '18 at 8:47






  • 2




    An example is not necessary ofc - but for the sake of a good community write 2 lines of code if you know how it works. This will improve the answer itself a lot. I previously gave this answer a upvote. Nevertheless I can ask for an example
    – Jonathan Stellwag
    Nov 13 '18 at 15:58



















1














Either by using javascript as following:






function numberWithSpaces(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, " ");
return parts.join(".");
}
var num = numberWithSpaces(123456789);
console.log(num);





This will output 123 456 789



Edit:



Typescript:






function numberWithSpaces(x) {
let parts = x.toString().split('.');
parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, ' ');
return parts.join('.');
}
let num = this.numberWithSpaces(123456789);
console.log(num);





add into your typescript file (.ts) which are related to your html which contain your <h2>item.price</h2> replace num with your item.price value.



Or using pipe.



You could see example on Stackblitz



Just simply call by: element.price = format.formatCurrency(element.price);



Once you've define the helper. For your usage you could define by:
<h2 *ngFor="let item of item">{{ item.price }}</h2>






share|improve this answer























  • how to use this function if I have <h2>{{item.price}}</h2>
    – Angulandy2
    Nov 13 '18 at 8:57










  • updated my answer. you need to add into your ts to define your price = num.
    – Mukyuu
    Nov 13 '18 at 9:16



















0














Make you own custom pipe:
https://toddmotto.com/angular-pipes-custom-pipes



In the transform function, convert it to a string like so:
Add commas or spaces to group every three digits






share|improve this answer





























    0














    Extremely simple way to do this:



    function formatNumber(num) {
    return num.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276902%2fangular-2-money-number-space-after-3-zeros%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      consider using a pipe that takes the number, convert it to a string and format it as you like.. you could add a currency two.



      example:(more of a point illustration than a working code.. can't test at the moment)



      //convert the string to an array
      var arr = str.split("");
      //back to string with added spaces
      let string = "";
      let count = 0;
      for(i=arr.length-1; i>=0; i--){
      if(count<3){
      string = arr[i] + string;
      count = count+1;
      }
      else{
      string = " " + string;
      count=0;
      i=i+1;
      }
      }





      share|improve this answer



















      • 1




        Can you provide an example please.
        – Jonathan Stellwag
        Nov 13 '18 at 8:41






      • 2




        @JonathanStellwag example is not necessary, because no code was given. The question was what to use for it. And a pipe is the best solution
        – PierreDuc
        Nov 13 '18 at 8:47






      • 2




        An example is not necessary ofc - but for the sake of a good community write 2 lines of code if you know how it works. This will improve the answer itself a lot. I previously gave this answer a upvote. Nevertheless I can ask for an example
        – Jonathan Stellwag
        Nov 13 '18 at 15:58
















      4














      consider using a pipe that takes the number, convert it to a string and format it as you like.. you could add a currency two.



      example:(more of a point illustration than a working code.. can't test at the moment)



      //convert the string to an array
      var arr = str.split("");
      //back to string with added spaces
      let string = "";
      let count = 0;
      for(i=arr.length-1; i>=0; i--){
      if(count<3){
      string = arr[i] + string;
      count = count+1;
      }
      else{
      string = " " + string;
      count=0;
      i=i+1;
      }
      }





      share|improve this answer



















      • 1




        Can you provide an example please.
        – Jonathan Stellwag
        Nov 13 '18 at 8:41






      • 2




        @JonathanStellwag example is not necessary, because no code was given. The question was what to use for it. And a pipe is the best solution
        – PierreDuc
        Nov 13 '18 at 8:47






      • 2




        An example is not necessary ofc - but for the sake of a good community write 2 lines of code if you know how it works. This will improve the answer itself a lot. I previously gave this answer a upvote. Nevertheless I can ask for an example
        – Jonathan Stellwag
        Nov 13 '18 at 15:58














      4












      4








      4






      consider using a pipe that takes the number, convert it to a string and format it as you like.. you could add a currency two.



      example:(more of a point illustration than a working code.. can't test at the moment)



      //convert the string to an array
      var arr = str.split("");
      //back to string with added spaces
      let string = "";
      let count = 0;
      for(i=arr.length-1; i>=0; i--){
      if(count<3){
      string = arr[i] + string;
      count = count+1;
      }
      else{
      string = " " + string;
      count=0;
      i=i+1;
      }
      }





      share|improve this answer














      consider using a pipe that takes the number, convert it to a string and format it as you like.. you could add a currency two.



      example:(more of a point illustration than a working code.. can't test at the moment)



      //convert the string to an array
      var arr = str.split("");
      //back to string with added spaces
      let string = "";
      let count = 0;
      for(i=arr.length-1; i>=0; i--){
      if(count<3){
      string = arr[i] + string;
      count = count+1;
      }
      else{
      string = " " + string;
      count=0;
      i=i+1;
      }
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 13 '18 at 9:04

























      answered Nov 13 '18 at 8:41









      Me1oMe1o

      2216




      2216








      • 1




        Can you provide an example please.
        – Jonathan Stellwag
        Nov 13 '18 at 8:41






      • 2




        @JonathanStellwag example is not necessary, because no code was given. The question was what to use for it. And a pipe is the best solution
        – PierreDuc
        Nov 13 '18 at 8:47






      • 2




        An example is not necessary ofc - but for the sake of a good community write 2 lines of code if you know how it works. This will improve the answer itself a lot. I previously gave this answer a upvote. Nevertheless I can ask for an example
        – Jonathan Stellwag
        Nov 13 '18 at 15:58














      • 1




        Can you provide an example please.
        – Jonathan Stellwag
        Nov 13 '18 at 8:41






      • 2




        @JonathanStellwag example is not necessary, because no code was given. The question was what to use for it. And a pipe is the best solution
        – PierreDuc
        Nov 13 '18 at 8:47






      • 2




        An example is not necessary ofc - but for the sake of a good community write 2 lines of code if you know how it works. This will improve the answer itself a lot. I previously gave this answer a upvote. Nevertheless I can ask for an example
        – Jonathan Stellwag
        Nov 13 '18 at 15:58








      1




      1




      Can you provide an example please.
      – Jonathan Stellwag
      Nov 13 '18 at 8:41




      Can you provide an example please.
      – Jonathan Stellwag
      Nov 13 '18 at 8:41




      2




      2




      @JonathanStellwag example is not necessary, because no code was given. The question was what to use for it. And a pipe is the best solution
      – PierreDuc
      Nov 13 '18 at 8:47




      @JonathanStellwag example is not necessary, because no code was given. The question was what to use for it. And a pipe is the best solution
      – PierreDuc
      Nov 13 '18 at 8:47




      2




      2




      An example is not necessary ofc - but for the sake of a good community write 2 lines of code if you know how it works. This will improve the answer itself a lot. I previously gave this answer a upvote. Nevertheless I can ask for an example
      – Jonathan Stellwag
      Nov 13 '18 at 15:58




      An example is not necessary ofc - but for the sake of a good community write 2 lines of code if you know how it works. This will improve the answer itself a lot. I previously gave this answer a upvote. Nevertheless I can ask for an example
      – Jonathan Stellwag
      Nov 13 '18 at 15:58













      1














      Either by using javascript as following:






      function numberWithSpaces(x) {
      var parts = x.toString().split(".");
      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, " ");
      return parts.join(".");
      }
      var num = numberWithSpaces(123456789);
      console.log(num);





      This will output 123 456 789



      Edit:



      Typescript:






      function numberWithSpaces(x) {
      let parts = x.toString().split('.');
      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, ' ');
      return parts.join('.');
      }
      let num = this.numberWithSpaces(123456789);
      console.log(num);





      add into your typescript file (.ts) which are related to your html which contain your <h2>item.price</h2> replace num with your item.price value.



      Or using pipe.



      You could see example on Stackblitz



      Just simply call by: element.price = format.formatCurrency(element.price);



      Once you've define the helper. For your usage you could define by:
      <h2 *ngFor="let item of item">{{ item.price }}</h2>






      share|improve this answer























      • how to use this function if I have <h2>{{item.price}}</h2>
        – Angulandy2
        Nov 13 '18 at 8:57










      • updated my answer. you need to add into your ts to define your price = num.
        – Mukyuu
        Nov 13 '18 at 9:16
















      1














      Either by using javascript as following:






      function numberWithSpaces(x) {
      var parts = x.toString().split(".");
      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, " ");
      return parts.join(".");
      }
      var num = numberWithSpaces(123456789);
      console.log(num);





      This will output 123 456 789



      Edit:



      Typescript:






      function numberWithSpaces(x) {
      let parts = x.toString().split('.');
      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, ' ');
      return parts.join('.');
      }
      let num = this.numberWithSpaces(123456789);
      console.log(num);





      add into your typescript file (.ts) which are related to your html which contain your <h2>item.price</h2> replace num with your item.price value.



      Or using pipe.



      You could see example on Stackblitz



      Just simply call by: element.price = format.formatCurrency(element.price);



      Once you've define the helper. For your usage you could define by:
      <h2 *ngFor="let item of item">{{ item.price }}</h2>






      share|improve this answer























      • how to use this function if I have <h2>{{item.price}}</h2>
        – Angulandy2
        Nov 13 '18 at 8:57










      • updated my answer. you need to add into your ts to define your price = num.
        – Mukyuu
        Nov 13 '18 at 9:16














      1












      1








      1






      Either by using javascript as following:






      function numberWithSpaces(x) {
      var parts = x.toString().split(".");
      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, " ");
      return parts.join(".");
      }
      var num = numberWithSpaces(123456789);
      console.log(num);





      This will output 123 456 789



      Edit:



      Typescript:






      function numberWithSpaces(x) {
      let parts = x.toString().split('.');
      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, ' ');
      return parts.join('.');
      }
      let num = this.numberWithSpaces(123456789);
      console.log(num);





      add into your typescript file (.ts) which are related to your html which contain your <h2>item.price</h2> replace num with your item.price value.



      Or using pipe.



      You could see example on Stackblitz



      Just simply call by: element.price = format.formatCurrency(element.price);



      Once you've define the helper. For your usage you could define by:
      <h2 *ngFor="let item of item">{{ item.price }}</h2>






      share|improve this answer














      Either by using javascript as following:






      function numberWithSpaces(x) {
      var parts = x.toString().split(".");
      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, " ");
      return parts.join(".");
      }
      var num = numberWithSpaces(123456789);
      console.log(num);





      This will output 123 456 789



      Edit:



      Typescript:






      function numberWithSpaces(x) {
      let parts = x.toString().split('.');
      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, ' ');
      return parts.join('.');
      }
      let num = this.numberWithSpaces(123456789);
      console.log(num);





      add into your typescript file (.ts) which are related to your html which contain your <h2>item.price</h2> replace num with your item.price value.



      Or using pipe.



      You could see example on Stackblitz



      Just simply call by: element.price = format.formatCurrency(element.price);



      Once you've define the helper. For your usage you could define by:
      <h2 *ngFor="let item of item">{{ item.price }}</h2>






      function numberWithSpaces(x) {
      var parts = x.toString().split(".");
      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, " ");
      return parts.join(".");
      }
      var num = numberWithSpaces(123456789);
      console.log(num);





      function numberWithSpaces(x) {
      var parts = x.toString().split(".");
      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, " ");
      return parts.join(".");
      }
      var num = numberWithSpaces(123456789);
      console.log(num);





      function numberWithSpaces(x) {
      let parts = x.toString().split('.');
      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, ' ');
      return parts.join('.');
      }
      let num = this.numberWithSpaces(123456789);
      console.log(num);





      function numberWithSpaces(x) {
      let parts = x.toString().split('.');
      parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, ' ');
      return parts.join('.');
      }
      let num = this.numberWithSpaces(123456789);
      console.log(num);






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 13 '18 at 9:24

























      answered Nov 13 '18 at 8:43









      MukyuuMukyuu

      7091619




      7091619












      • how to use this function if I have <h2>{{item.price}}</h2>
        – Angulandy2
        Nov 13 '18 at 8:57










      • updated my answer. you need to add into your ts to define your price = num.
        – Mukyuu
        Nov 13 '18 at 9:16


















      • how to use this function if I have <h2>{{item.price}}</h2>
        – Angulandy2
        Nov 13 '18 at 8:57










      • updated my answer. you need to add into your ts to define your price = num.
        – Mukyuu
        Nov 13 '18 at 9:16
















      how to use this function if I have <h2>{{item.price}}</h2>
      – Angulandy2
      Nov 13 '18 at 8:57




      how to use this function if I have <h2>{{item.price}}</h2>
      – Angulandy2
      Nov 13 '18 at 8:57












      updated my answer. you need to add into your ts to define your price = num.
      – Mukyuu
      Nov 13 '18 at 9:16




      updated my answer. you need to add into your ts to define your price = num.
      – Mukyuu
      Nov 13 '18 at 9:16











      0














      Make you own custom pipe:
      https://toddmotto.com/angular-pipes-custom-pipes



      In the transform function, convert it to a string like so:
      Add commas or spaces to group every three digits






      share|improve this answer


























        0














        Make you own custom pipe:
        https://toddmotto.com/angular-pipes-custom-pipes



        In the transform function, convert it to a string like so:
        Add commas or spaces to group every three digits






        share|improve this answer
























          0












          0








          0






          Make you own custom pipe:
          https://toddmotto.com/angular-pipes-custom-pipes



          In the transform function, convert it to a string like so:
          Add commas or spaces to group every three digits






          share|improve this answer












          Make you own custom pipe:
          https://toddmotto.com/angular-pipes-custom-pipes



          In the transform function, convert it to a string like so:
          Add commas or spaces to group every three digits







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 8:46









          Rutger van DijkRutger van Dijk

          867




          867























              0














              Extremely simple way to do this:



              function formatNumber(num) {
              return num.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
              }





              share|improve this answer


























                0














                Extremely simple way to do this:



                function formatNumber(num) {
                return num.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
                }





                share|improve this answer
























                  0












                  0








                  0






                  Extremely simple way to do this:



                  function formatNumber(num) {
                  return num.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
                  }





                  share|improve this answer












                  Extremely simple way to do this:



                  function formatNumber(num) {
                  return num.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 8:47









                  Jack BashfordJack Bashford

                  5,79131235




                  5,79131235






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276902%2fangular-2-money-number-space-after-3-zeros%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine