Noob needs some formatting help...

Discussion in 'Plugin Development' started by Flenix, May 14, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Flenix

    Hey guys,

    Got a really simple one here really just I can't work out how to do it.

    Say I had a command /xpshow, that would show XP, using ".getExp()" in the plugin. However when its done, the XP is shown as for example 0.4999999999 - How would I make that instead appear as just "49%" (Only 2 decimels, and hiding the fact its a decimel, and adding the % sign)

    Any help would be great :) If you could explain how to do it that'd be better, because I'm having a similar issue in a different place too (relating to Vault-based money, and displaying time)
     
  2. Offline

    Flash619

    I found something kinda similar:
    Code:java
    1.  
    2. NumberFormat nf = NumberFormat.getPercentInstance();
    3. System.out.println(nf.format(0.47));
    4.  

    Found here: http://www.iro.umontreal.ca/~vaucher/Java/tutorials/Formatting.html
    It prints out 47%

    If you have to limit the double down to two decimal you can use number formatting like:
    Code:java
    1.  
    2. double roundTwoDecimals(double d) {
    3. DecimalFormat twoDForm = new DecimalFormat("#.##");
    4. return Double.valueOf(twoDForm.format(d));
    5. }
    6.  

    The above code if sent a decimal of .5857777 will return .58

    Then you can look into converting a double into a percentage similar to what I linked above?

    Will that work?

    This may also do something:
    Code:java
    1.  
    2. import java.text.DecimalFormat;
    3.  
    4. public class Main {
    5.  
    6. public static void main(String[] argv) throws Exception {
    7.  
    8.  
    9. System.out.println(df.format(0.19));
    10.  
    11. System.out.println(df.format(-0.19));
    12.  
    13. }
    14.  
    15. }
    16.  
    17. /*19%
    18.  
    19. -19%
    20.  
    21. */
    22.  

    It may be possible to use the (df.format(MyDecimal)) in other locations other than a print?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  3. Offline

    Flenix

    The fact it has a % sign in at all makes the server cry:

    Code:
     02:16:22 [SEVERE] java.util.UnknownFormatConversionException: Conversion = '%'
    02:16:22 [SEVERE] at java.util.Formatter.checkText(Unknown Source)
    02:16:22 [SEVERE] at java.util.Formatter.parse(Unknown Source)
    02:16:22 [SEVERE] at java.util.Formatter.format(Unknown Source)
    02:16:22 [SEVERE] at java.util.Formatter.format(Unknown Source)
    02:16:22 [SEVERE] at java.lang.String.format(Unknown Source)
    02:16:22 [SEVERE] at me.Flenix.mmoInfoEXP$CustomLabel.onTick(mmoInfoEXP.java:75)
    02:16:22 [SEVERE] at org.getspout.spoutapi.gui.GenericScreen.onTick(GenericScreen.java:140)
    02:16:22 [SEVERE] at org.getspout.spoutapi.gui.InGameScreen.onTick(InGameScreen.java:69)
    02:16:22 [SEVERE] at org.getspout.spout.player.SpoutCraftPlayer.onTick(SpoutCraftPlayer.java:1162)
    02:16:22 [SEVERE] at org.getspout.spout.ServerTickTask.run(ServerTickTask.java:41)
    02:16:22 [SEVERE] at org.bukkit.craftbukkit.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:126)
    02:16:22 [SEVERE] at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:588)
    02:16:22 [SEVERE] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:506)
     02:16:22 [SEVERE] at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    And for reference:
    Code:
     public void onTick() {
    if (check) {
    DecimalFormat dformat = new DecimalFormat("#%");
      setText(String.format("EXP: " + (dformat.format(getScreen().getPlayer().getExp()))));
    The last line is 75
     
  4. Offline

    Flash619

    I'm not entirely sure then. If you find anything let me know though because this seems to be a near impossible thing to find on google.

    Actually first convert it to two decimal places, then try:
    Code:java
    1.  
    2. Double myInt = 0.1234;
    3. Console.WriteLine( myInt.ToString( "P", nfi ) ); //writes out the number with a %
    4.  
    5. //mind you you would be using well... could use
    6.  
    7. String MyString = myInt.ToString( "P", nfi ) ;
    8.  

    as seen here:
    http://www.velocityreviews.com/forums/t106173-how-to-convert-double-to-percent.html

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
Thread Status:
Not open for further replies.

Share This Page