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 ;)

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.

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).

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

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

When you reach that level of frustration it's time for a break!

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)

Seriously - time for a break. Trust me!

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)......

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

Got it. The only thing is that I want it to be stored as 32,00 instead of 32,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?

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

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.

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

What is the best way to do this?

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

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

Already answered (17 hours ago).

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.

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.

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.

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.

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

commented: Java is completely crap. Thank you. +6

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....

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.

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

Thought I marked it as solve.

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

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,

commented: Where were you when this thread was on? Just a post to get the thread answered by you. Voted down. -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.