[Free / Open-Source] Number Formatting - my first extension! version 3.0

My first extension - number formatting.


Hi this is my first extension! hope you enjoy it
Publish date: 2020-10-19T19:20:00Z
Current version: 2
Current version publish date:2020-10-24T21:00:00Z

Write a short description


This is a simple extension that you can use to format numbers to default formats, or to a custom one

P.S. this is my first extension, so it is very simple. you can do a procedure to format numbers.

Add a picture of all the blocks



Format
Formats a number to a default format.
number ~ int ~ number to be formatted, in meters, bytes or grams
format format to:

  1. cm
  2. km
  3. GB
  4. MB
  5. kg

CustomFormat
customize the formatting of a number.
number ~ int ~ the number to be formatted
prefix and sufix ~ Strings ~ prefix or sufix your number with anything. leave an empty string if you don’t want to prefix/sufix
multiplyFor ~ int ~ multiply your number for other. if you don’t want to use this, put 1.
Separate
put a separator into your number.
number ~ the number you want to add separator
separator ~ the separator you want to use in your number
Indirect Methods
These are methods that will raise the indirect formatting event with the value instead of returning it. useful if you want to do something after formatting.
Error
error Message ~ int
an error occured
1 = you must choose a valid default format: Direct Method
2 = you must choose a valid default format: Indirect Method
IndirectFormatting
Event raised when you use indirect methods
value ~ the return value of the method.

Include a Download link here


Prior Versions

version 1
com.appybuilder.hguim150.Number_formatter.aix (6.4 KB)
version 2
com.appybuilder.hguim150.Number_formatter.aix (7.8 KB)

Version 3
AIX:

com.appybuilder.hguim150.Number_formatter.aix (7.8 KB)

Hope you liked!!!

  • 1
  • 2
  • 3
  • 4
  • 5

0 voters

16 Likes

Good start ! Really nice extension :+1:


Btw , the title says it’s open source, even though I didn’t found Any source :grin:
Edit: thanks!
Also please follow the naming conventions:

4 Likes

thanks for the reply and for all the help you gave me!

2 Likes

i think i thinked open source was a totally different thing… sorry!

2 Likes

Very nice start!

1 Like

thanks i even tried to add you to help building my first extension but it doesn’t allowed with you. Thanks anyways! :smiley:

1 Like

Hmmm… I didn’t deny any invites or disable private message.

if you want i can give

Great extension… Keep koding

import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.common.*;

@DesignerComponent(version = 1,  description = "Format numbers in multiple formats",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,   iconName = "http://appyBuilder.com/extensions/icons/extension.png")
@SimpleObject(external = true)
public class Number_formatter extends AndroidNonvisibleComponent {
  private ComponentContainer container;
  public Number_formatter(ComponentContainer container) {
      super(container.$form());
      this.container = container;
  }
  @SimpleEvent (description = "An error occured")
  public void Error(int errorMessage) {
    EventDispatcher.dispatchEvent(this, "Error", errorMessage);
  }
  @SimpleFunction(description = "Formats an integer to any format")
  public String Format(int number, int format) {
    if (format == 1) {
      return (number/100) + "cm";
    }
    else if (format == 2) {
      return (number*1000) + "km";
    }
    else if (format == 3) {
      return (number/1000000000) + "GB";
    }
    else if (format == 4) {
      return (number/1000000) + "MB";
    }
    else if (format == 5) {
      return (number*1000) + "kg";
    }
    else {
      error(1);
      return "";
    }
}
1 Like

@GuiM_Hares, it is a very nice extension.

Keep koding…
All the best.

If you have provided the code, this is an open-source!

2 Likes

Version 2 Code:

import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.common.*;

@DesignerComponent(version = 2,  description = "Format numbers in multiple formats",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,   iconName = "http://appyBuilder.com/extensions/icons/extension.png")
@SimpleObject(external = true)
public class Number_formatter extends AndroidNonvisibleComponent {
  private ComponentContainer container;
  public Number_formatter(ComponentContainer container) {
      super(container.$form());
      this.container = container;
  }
  @SimpleEvent (description = "An error occured")
  public void Error(int errorMessage) {
    EventDispatcher.dispatchEvent(this, "Error", errorMessage);
  }
  @SimpleEvent (description = "Event triggered when you request an indirect formatting")
  public void IndirectFormatting(String value) {
    EventDispatcher.dispatchEvent(this, "IndirectFormatting", value);
  }
  @SimpleFunction(description = "Formats an integer to any format")
  public String Format(int number, int format) {
    if (format == 1) {
      return (number/100) + "cm";
    }
    else if (format == 2) {
      return (number*1000) + "km";
    }
    else if (format == 3) {
      return (number/1000000000) + "GB";
    }
    else if (format == 4) {
      return (number/1000000) + "MB";
    }
    else if (format == 5) {
      return (number*1000) + "kg";
    }
    else {
      Error(1);
      return "";
    }
  }
  @SimpleFunction(description = "customize the formatting of a number")
  public String CustomFormat(int number, String prefix, String sufix, int multiplyFor) {
     return (prefix + (number * multiplyFor)) + sufix;
  }
  @SimpleFunction(description = "put separators in numbers")
  public String Separate(int number, String separator) {
    String formattednumber = String.format(("%" + separator) + "d", number);
    return formattednumber;
  }
  @SimpleFunction(description = "trigger the Indirect formatting event and formatt to a default format")
  public void IndirectFormat (int number, int format) {
   if (format == 1) {
     IndirectFormatting ((number/100) + "cm");
   }
   else if (format == 2) {
     IndirectFormatting ((number*1000) + "km");
   }
   else if (format == 3) {
     IndirectFormatting((number/1000000000) + "GB");
   }
   else if (format == 4) {
     IndirectFormatting((number/1000000) + "MB");
   }
   else if (format == 5) {
     IndirectFormatting((number*1000) + "kg");
   }
   else {
     Error(2);
   }
  }
  @SimpleFunction(description = "trigger the Indirect formatting event and formats to a custom format")
  public void IndirectCustomFormat (int number, String prefix, String sufix, int multiplyFor) {
    IndirectFormatting((prefix + (number * multiplyFor)) + sufix);
  }
  @SimpleFunction(description = "trigger the Indirect formatting event and add separator to a number")
  public void IndirectSeparator(int number, String separator) {
    String formattednumber = String.format(("%" + separator) + "d", number);
    IndirectFormatting(formattednumber);
  }
}
2 Likes

version 3 released!!! now, it will alow you to use dot or any other char as separator. There was a bug in version 2 that didn’t allowed to do that.

code:

import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.common.*;

@DesignerComponent(version = 3,  description = "Format numbers in multiple formats",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,   iconName = "http://appyBuilder.com/extensions/icons/extension.png")
@SimpleObject(external = true)
public class Number_formatter extends AndroidNonvisibleComponent {
  private ComponentContainer container;
  public Number_formatter(ComponentContainer container) {
      super(container.$form());
      this.container = container;
  }
  @SimpleEvent (description = "An error occured")
  public void Error(int errorMessage) {
    EventDispatcher.dispatchEvent(this, "Error", errorMessage);
  }
  @SimpleEvent (description = "Event triggered when you request an indirect formatting")
  public void IndirectFormatting(String value) {
    EventDispatcher.dispatchEvent(this, "IndirectFormatting", value);
  }
  @SimpleFunction(description = "Formats an integer to any format")
  public String Format(int number, int format) {
    if (format == 1) {
      return (number/100) + "cm";
    }
    else if (format == 2) {
      return (number*1000) + "km";
    }
    else if (format == 3) {
      return (number/1000000000) + "GB";
    }
    else if (format == 4) {
      return (number/1000000) + "MB";
    }
    else if (format == 5) {
      return (number*1000) + "kg";
    }
    else {
      Error(1);
      return "";
    }
  }
  @SimpleFunction(description = "customize the formatting of a number")
  public String CustomFormat(int number, String prefix, String sufix, int multiplyFor) {
     return (prefix + (number * multiplyFor)) + sufix;
  }
  @SimpleFunction(description = "put separators in numbers")
  public String Separate(int number, String separator) {
    String formattednumber = String.format("%,d", number);
    return formattednumber.replace(",",separator);
  }
  @SimpleFunction(description = "trigger the Indirect formatting event and formatt to a default format")
  public void IndirectFormat (int number, int format) {
   if (format == 1) {
     IndirectFormatting ((number/100) + "cm");
   }
   else if (format == 2) {
     IndirectFormatting ((number*1000) + "km");
   }
   else if (format == 3) {
     IndirectFormatting((number/1000000000) + "GB");
   }
   else if (format == 4) {
     IndirectFormatting((number/1000000) + "MB");
   }
   else if (format == 5) {
     IndirectFormatting((number*1000) + "kg");
   }
   else {
     Error(2);
   }
  }
  @SimpleFunction(description = "trigger the Indirect formatting event and formats to a custom format")
  public void IndirectCustomFormat (int number, String prefix, String sufix, int multiplyFor) {
    IndirectFormatting((prefix + (number * multiplyFor)) + sufix);
  }
  @SimpleFunction(description = "trigger the Indirect formatting event and add separator to a number")
  public void IndirectSeparator(int number, String separator) {
    String formattednumber = String.format("%,d", number);
    IndirectFormatting(formattednumber.replace(",",separator));
  }
}
4 Likes

formating number with java

here is :

num1 = 123456789;
output1 = num1.toLocaleString(‘en-US’);
document.all[0].innerText,output1


here is local arabic number if using arabic keyboard

code:
num1 = 123456789;
output1 = parseInt( num1 ).toLocaleString() ;

document.all[0].innerText,output1

OR SEE THIS