0

There are loads of ways to do this using standard methods from the String class, or char arrays, or regular expressions (etc). I'm not going to deprive you of the pleasure of finding one for yourself ;)

0

There are loads of ways to do this using standard methods from the String class, or char arrays, or regular expressions (etc). I'm not going to deprive you of the pleasure of finding one for yourself ;)

Iv obviously used the String class (where would I get the parse float function) so I dont see much other functions that can do this
I dont think going from String to a char array removing it and then stringing it again, is the best way to do this but if there is no other way, what choice do I have
Im not into regular expressions.

0

Going through it one char at a time isn't such a bad thing. OK, maybe someone knows a really slick way, but often the most obvious solution is the one to use. You could collect all the digits before the decimal into one string, all the digits after the decimal into another, concatenate those together with a '.' inbetween, and that would be good input for parseDouble. (There's a method in Char to tell if a char is a digit).

0
char[] charprice=price.toCharArray();
charprice[price.indexOf(symbol)]='';

This and so many reasons is why Java sucks. Why doesnt this work?

0

Fucking incredible

        String price="16,00 €"; //Price
        int multiply=2; //Multiplcation that I want to do to price
        int positions=0; //Position of the symbol
        int positiond=0; //Position of the decimal
        char[] symbols = new char[] { '€','$' }; //Symbols that are avaliable
        char[] decimalseperators= new char[] { '.',',' }; // Decimal markers that are avaliable
        char symbol=' '; //Somewhere to store the symbol
        char deci=' '; //Somewhere to store the decimal
        String blank="";
        boolean pre; //Check if the decimal is before or after the the symbol




        for (int i=0;i<decimalseperators.length;i++) //I run thru the entire decimal array and the price itself to look for the decimal
        {
            deci=decimalseperators[i];
            if (price.indexOf(deci)  >=0)
            {
                if (deci!='.') //If the decimal is not a dot, for compatibility reasons, I change it to a dot
                {

                    StringBuilder newprice = new StringBuilder(price);
                    newprice.setCharAt(price.indexOf(deci), '.');
                    price=newprice.toString();
                }
                positiond=price.indexOf(deci); //I store the position of the decimal point
                break;
            }

        }

        for (int i=0;i<symbols.length;i++) //I run thru the entire symbol array and the price itself to look for the symbol
        {
            symbol=symbols[i];
             if (price.indexOf(symbol) >= 0) 
             {
                 char[] charprice=price.toCharArray();
                 StringBuilder sb = new StringBuilder();
                 sb.append(charprice);
                 sb.deleteCharAt(price.indexOf(symbol));
                 charprice = sb.toString().toCharArray();
                 price=charprice.toString();             



                 positions=price.indexOf(symbol); //I store the position of the symbol
                 break;
             }
        }

        if (positions >positiond) //If symbol after the decimal
         {
             pre=false;

         }
        else //If the symbol is before the decimal
        {
            pre=true;
        }
        float result=Float.valueOf(price)*Float.valueOf(String.valueOf(multiply)); //I store it as a float for decimal reasons



        if (pre==false) //Put it all together again
        {
            price=result+" "+symbol;
        }
        else
        {
            price=symbol+" "+result;
        }

        System.out.println(price); //Print it out
        System.exit(1);

Just fucking incredible (Code doesnt does not work)

0

Seriously - time for a break. Trust me!

Im on my break :P

Pretty much nothing to do except figure out how to do this and move on with the next thing (which I cant do without solving this)......

0

Don't forget to use lots of print statements so you can debug your code, eg
at lines 32, 44 (etc) print the price String to see if it's been updated as you expected

0

Got it. The only thing is that I want it to be stored as 32,00 instead of 32,0

Edited by riahc3

0

float/double are stored as 32/64 bits regardless. I guess you mean you want the result formatted to the same number of decimals as the source?

0

Yes, basically. 2 digits to the right of the decimal point, infinite to its left.

0

If you know its 2 dec places you can use standard Java NumberFormat to convert your result to string, then fix the decimal separator and add the currency.

0

It may or may not be. I just want 2 decimal places to the righrt.

0

What is the best way to do this?

Annoying when you have been stuck almost a day and something...

0

So whats the best way to make sure those two decimal points appear?

0

Already answered (17 hours ago).

double result=Double.valueOf(price)*Double.valueOf(String.valueOf(multiply)); //I store it as a float for decimal reasons
        NumberFormat nb=NumberFormat.getInstance();
        nb.setMinimumFractionDigits(2);
        nb.setMaximumFractionDigits(2);
        nb.format(result);
        System.out.println(result);

Nope, not answered. Still shows without 2 digits to the right of the decimal point.

1

I'm worried. This thread has become very unheathy - just look at the number and kind of questions that are being asked. I know you have the Java skills to answer many of them yourself, and I don't think I'm providing the right kind of help any more.

So this may br my last post in the thread (maybe)...
Yes. Answered. What makes you think that that code will do anything other than print result (a double) in a system default format?
If you don't know how to use a NumberFormat then read the API doc, read the examples, Google if necessary. Don't just guess. The mistake in your code is a very elementary one.

Edited by JamesCherrill

0

I'm worried. This thread has become very unheathy - just look at the number and kind of questions that are being asked. I know you have the Java skills to answer many of them yourself, and I don't think I'm providing the right kind of help any more.

So this may br my last post in the thread (maybe)...
Yes. Answered. What makes you think that that code will do anything other than print result (a double) in a system default format?
If you don't know how to use a NumberFormat then read the API doc, read the examples, Google if necessary. Don't just guess. The mistake in your code is a very elementary one.

......ooooooooooooo............kkkkkkkkkkk............."Unhealthy"?

Anyways, I looked up that NumberFormat and saw how it works. http://www.avajava.com/tutorials/lessons/how-do-i-use-numberformat-to-format-doubles.html?page=1

As far as I can see, it is set up correctly.

Java's API has always been basically a cat that has been ran over by a truck on a highway: Dead crap. Looked it up anyways and it basically told me nothing that example page didnt tell me and explained it better.

0

Also, if you basically told me what is the elementary mistake in the code, this thread would end. I got the symbol, the decimal thing working.......pretty much nothing else to do except display two decimal positions always.

1

What did you do with the String that the format method returns? I KNOW you are a better Java programmmer than this.

Votes + Comments
Java is completely crap. Thank you.
0

What did you do with the String that the format method returns? I KNOW you are a better Java programmmer than this.

Another reason why Java is ****; I thought It automatically changed it (which would be logic but then again Java isnt logic...), it wasnt suppose to return another String....

Edited by JamesCherrill: No obscene language

0

Anyways thats it. Thank you JamesCherrill as you are the only one that truely helped.

Also, I deal almost on a daily base with PHP, JS, C#, Java, shell scripting.....and on top of that I hate programing so these elementary mistakes slip me.

Edited by riahc3

0

It must be hard, working all day with such a range of languages when really you don't enjoy it. Maybe you can use it as a springboard for moving on to something that you will enjoy more? I know I've had jobs where it was hard to tear myself away in the evenings, and others where it was hard to get up in the morning, and I know which I preferred!
Anyway, if "that's it" then please mark this solved.
J

0

Thought I marked it as solve.

Yes, Im trying to get a cert that has nothing to do with programming (Computers, but not programming)

-1

conclusion

  • use Grouping in Regex

    1. grouping in Big Figures (000 000 / 000,000), incl. indian numbers formatter (1,00,000) use ISO standards

    2. decimal separator, grouping separator use ISO standards

    3. number and currecny formats (west formatting, arabic, hindu, chinese, japan ... ending with indian) use ISO standards

    4. number of decimal places is different CCy by CCy, (JPY, CHF etc...) use ISO standards

    5. up to 64 conditions for whole (Standard ISO Currency) world, simple binary tree with additional/conditionals nodes

  • if value passed then output should be a number in double form and in proper Java Currency format

  • Double/Float haven't decimal places, most important for all calculations for MM and Security market, difference will be significant,

Edited by mKorbel

Votes + Comments
Where were you when this thread was on? Just a post to get the thread answered by you. Voted down.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.