Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Java Primitive Type Conversion Elegance

Name: Cup.Of.Joe 2007-01-16 16:40

Dear programmers,

public class Convertion {

  public static void main(String[] args) {

    final float CONVERTION_RATE = 200.482F;
    float given_number = 3772.46F;

    System.out.println("€" + given_number + " = " +
                      (int)(given_number * CONVERTION_RATE) + " Escudos");
  }
}

This is a little code I made to convert a given number of euros (3772,46 in this case) to portuguese 'escudos'.
I would like to make the primitive type convertion more elegant.
Since the 'escudo' lost its fractional value, I think that some floor/ceiling thing it's probably needed here.

(Yeah, this is a 4 years-old programming exercise, made when the Euro was dandy...)

Thank you, programmers.

Name: Anonymous 2007-01-17 11:16

There is no such thing as elegance in Java, only scalable web 2.0 enterprise cluster

Name: Anonymous 2007-01-17 20:18 (sage)

It's XXX€, not €XXX like dollars.

Name: Anonymous 2007-01-18 4:11

>>3

Actually, either is valid

Name: Anonymous 2007-01-18 9:08 (sage)

And €x is much more common.

Name: Anonymous 2007-01-18 9:46

x€ is more common here

Name: Anonymous 2007-01-18 11:28 (sage)

>>6
no one cares about your ass-backwards country

Name: Anonymous 2007-01-19 1:36

>>7

no one cares about your ass-forwards country

Name: Anonymous 2007-01-19 5:10

When something is 2 metres long (or 2 Koala noses, or whatever retarded measure Americans have), do you say "m 2" or "2 m"?

When you say something is "2 metres", do say "it's two metres" or "it's metres two"?

Name: Anonymous 2007-01-19 9:36 (sage)

>>9
Amerikans use Furlongs for measurements IIRC.

Name: Anonymous 2007-01-20 9:25

>>9
itss metter not metree yuo iddiott

Name: Anonymous 2007-01-20 11:26

Here is a better version of your code, faster, more evolutive and elegant:

(defun escudos->euros (n)
  (* 200.482 n))

(setf given_number 3772.46)
(format t "~d Euros = ~d Escudos"
        given_number
        (escudos->euros given_number))

Name: Anonymous 2007-01-20 11:27

>>10
Convertion is easy!!! 25.34 furlongs = 123.4439 inches
and 74635.23 inches = 1 koala nose = 123463555.32378 texas tea spoons.

Name: Anonymous 2007-01-20 11:28

>>4
>>5
actually NO, the Euro sign is written AFTER the number, that's all.

Name: Anonymous 2007-01-20 11:29

loooooooool Assmericans.

Name: Anonymous 2007-01-20 15:49

lol thread sabotage

Name: Anonymous 2007-01-20 16:31

>>16
you're too late again anonymous

Name: Anonymous 2007-01-20 20:23

>>13
I lol'd

Name: Anonymous 2007-01-22 16:52

import java.math.BigDecimal;
import java.math.MathContext;

public class Main {
  
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // java 1.4.2 solution
        final float CONVERTION_RATE = 200.482F;
        float given_number = 3772.46F;

        System.out.println("€" + given_number + " = " +
                          (((float)Math.round(100 * given_number * CONVERTION_RATE))/100) + " Escudos");
           
        // java 5 solution      
        BigDecimal convRate = new BigDecimal("200.482");
        BigDecimal ammountCur = new BigDecimal("3772.46");
       
        BigDecimal result = ammountCur.multiply(convRate);
        System.out.println("Raw : ");
        System.out.println("\tresult == " + result.toString());
        System.out.println("Rounded : ");
        System.out.println("\tresult == " + result.round(MathContext.DECIMAL32).toString());
    }
   
}

The J2SE 5 solution is better. It allows you to keep the precision internally and then round directly on the BigDecimal.
You can also set the rounding rules using a MathContext, something you can't really do elegantly in J2SE 1.4.2.
[You could use BigDecimal in the J2SE 1.4.2 solution but you would still need to convert back to the float primitive type before rounding.... nasty. No wonder they improved BigDecimal]

Name: Anonymous 2008-01-25 20:40

>>7
I am pleased that my ass is backwards.

Name: Anonymous 2008-01-25 20:55

IT'S SPELLED CONVERSION, YOU IDIOTS

Name: Anonymous 2008-01-26 1:28

>>21
YHBT. YHL. HAND.

Name: Anonymous 2008-01-26 1:53

>>1
your code is beautiful, I learned volumes from it

Name: Anonymous 2008-01-26 6:16

Estúpido!
Nessa altura bastava ir ao google e meter: 3772.46 PTE in EUR
Agora já não dá mas podes ver que o dólar é merda* com: 1 EUR in USD
ou 1 € in $

*dólar é merda = dollar is shit

Name: Anonymous 2008-01-26 6:19

IT'S CONVERSION

Name: Anonymous 2008-01-26 7:04

IT'S CONVERTHE GAMEION

Name: Anonymous 2008-01-26 7:22

CONVERTION
I lol'd

Name: Anonymous 2008-01-26 7:55

Do it like a nigger would, just wget a request to Google and sed the answer.

Name: Anonymous 2008-01-26 10:18

Yeah, this is a 4 years-old programming exercise

If it's meant for 4 year olds, why are you having so much trouble with it?

Name: Anonymous 2008-01-26 11:29

maybe he's only 3 years old

Name: Anonymous 2008-01-26 18:35

>>30
Maybe >>1 is Madeline McCann.

She's an EXPERT ABDUCTEE

Stop posting on /prog/ and come home.

Name: Anonymous 2009-03-18 3:44

I'm feeling really keen, for some of that good ol' green

Marijuana MUST be legalized.

Name: Trollbot9000 2009-07-01 7:56


convert a given number CONVERTION RATE Escudos.

Name: Anonymous 2011-02-03 4:13

Name: Anonymous 2011-02-03 7:00

Name: Sgt.Kabu論頺kiman舞ⱪ 2012-05-28 21:51

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List