Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

conversion prix (Float) vers String

8 réponses
Avatar
une.bevueVOTEZ
j'ai un "Price" qui se décompose en :

Float amount;
Integer digits;
String currency;

et je voudrais le convertir en une string du genre :

25.12 ¤

avec le nombre de chiffres à droite du point égal à digits.

comment faire ?

--
une bévue

8 réponses

Avatar
une.bevueVOTEZ
Une bévue wrote:

j'ai un "Price" qui se décompose en :

Float amount;
Integer digits;
String currency;


pour l'instant, j'ai fait ça :

public static String digitsToPattern(Integer digits) {
String pattern = "###";
int dig = digits.intValue();
if (dig > 0) {
pattern += ".";
for (int i = 0; i < dig; i++) {
pattern += "0";
}
}
return pattern;
}

public static String priceToString(Float amount, Integer digits,
String currency) {
String pattern = digitsToPattern(digits);
DecimalFormat myFormatter = new DecimalFormat(pattern);
return myFormatter.format(amount) + " " + currency;
}


"ça marche" mais, y a t'il plus court ?
d'autant que je n'ai pas encore traité de Locale...
--
une bévue

Avatar
Bruno CAUSSE
dans l'article 1gyls07.38m9c81d9plx1N%, Une bévue à
a écrit le 23/06/05 9:38 :

Une bévue wrote:

j'ai un "Price" qui se décompose en :

Float amount;
Integer digits;
String currency;


pour l'instant, j'ai fait ça :

public static String digitsToPattern(Integer digits) {
String pattern = "###";
int dig = digits.intValue();
if (dig > 0) {
pattern += ".";
for (int i = 0; i < dig; i++) {
pattern += "0";
}
}
return pattern;
}

public static String priceToString(Float amount, Integer digits,
String currency) {
String pattern = digitsToPattern(digits);
DecimalFormat myFormatter = new DecimalFormat(pattern);
return myFormatter.format(amount) + " " + currency;
}


"ça marche" mais, y a t'il plus court ?
d'autant que je n'ai pas encore traité de Locale...


Class DecimalFormat

 void setMaximumFractionDigits(int newValue)
Sets the maximum number of digits allowed in the fraction portion of a
number.

 void setMaximumIntegerDigits(int newValue)
Sets the maximum number of digits allowed in the integer portion of a
number.

 void setMinimumFractionDigits(int newValue)
   Sets the minimum number of digits allowed in the fraction portion of a
number.

 void setMinimumIntegerDigits(int newValue)
Sets the minimum number of digits allowed in the integer portion of a
number.


Avatar
Rakotomandimby (R12y) Mihamina
(Une bévue) :

comment faire ?


ça ressemble à un devoir... :-)

sans chercher de doc, tu peux faire une méthode qui transforme les
chiffres en String dans un float, à coup de division par 10 pour avoir
les unités, dizaines, centaines...

mais comme tu as internet :

il existe, au moins pour les entiers

Integer.toString(unEntier)
String.valueOf(int)

à toi de chercher pour les floats...

--
Miroir de logiciels libres http://www.etud-orleans.fr
Développement de logiciels libres http://aspo.rktmb.org/activites/developpement
Infogerance de serveur dédié http://aspo.rktmb.org/activites/infogerance
(En louant les services de l'ASPO vous luttez contre la fracture numerique)

Avatar
Bruno CAUSSE
dans l'article BEE03511.5C65%, Bruno CAUSSE à
a écrit le 23/06/05 9:44 :

dans l'article 1gyls07.38m9c81d9plx1N%, Une bévue à
a écrit le 23/06/05 9:38 :

Une bévue wrote:

j'ai un "Price" qui se décompose en :

Float amount;
Integer digits;
String currency;


pour l'instant, j'ai fait ça :

public static String digitsToPattern(Integer digits) {
String pattern = "###";
int dig = digits.intValue();
if (dig > 0) {
pattern += ".";
for (int i = 0; i < dig; i++) {
pattern += "0";
}
}
return pattern;
}

public static String priceToString(Float amount, Integer digits,
String currency) {
String pattern = digitsToPattern(digits);
DecimalFormat myFormatter = new DecimalFormat(pattern);
return myFormatter.format(amount) + " " + currency;
}


"ça marche" mais, y a t'il plus court ?
d'autant que je n'ai pas encore traité de Locale...


Class DecimalFormat

 void setMaximumFractionDigits(int newValue)
Sets the maximum number of digits allowed in the fraction portion of a
number.

 void setMaximumIntegerDigits(int newValue)
Sets the maximum number of digits allowed in the integer portion of a
number.

 void setMinimumFractionDigits(int newValue)
   Sets the minimum number of digits allowed in the fraction portion of a
number.

 void setMinimumIntegerDigits(int newValue)
Sets the minimum number of digits allowed in the integer portion of a
number.



Voir aussi

 void setCurrency(Currency currency)
Sets the currency used by this number format when formatting currency
values.



Avatar
Kupee
Une bévue wrote:
j'ai un "Price" qui se décompose en :

Float amount;
Integer digits;
String currency;

et je voudrais le convertir en une string du genre :

25.12 ¤

avec le nombre de chiffres à droite du point égal à digits.

comment faire ?


Tiens moi je propose ca, un peu tordu pour le digits mais ca marche
apparament
MessageFormat format = new MessageFormat("{0,number}.{1,number} {2}");
String msg = "25.127 ¤";
Object[] o = format.parse(msg);
float amount = ((Number)o[0]).intValue();
int cent = ((Number) o[1]).intValue();
int digits = (int) Math.ceil(Math.log10(cent));
amount += cent/Math.pow(10,digits);
String currency = (String) o[2];

Avatar
une.bevueVOTEZ
Kupee wrote:

Tiens moi je propose ca, un peu tordu pour le digits mais ca marche
apparament
MessageFormat format = new MessageFormat("{0,number}.{1,number} {2}");
String msg = "25.127 ¤";
Object[] o = format.parse(msg);
float amount = ((Number)o[0]).intValue();
int cent = ((Number) o[1]).intValue();
int digits = (int) Math.ceil(Math.log10(cent));
amount += cent/Math.pow(10,digits);
String currency = (String) o[2];


ok, merci à tous, j'ai trouvé "mon bonheur" ;-)
--
une bévue

Avatar
Christophe Tela
Une bévue wrote:

j'ai un "Price" qui se décompose en :

Float amount;
Integer digits;
String currency;


Pour stocker un prix, mieux vaux utiliser un BigDecimal. Avec un float, tu
t'exposes à des problèmes d'arrondi.

Avatar
une.bevueVOTEZ
Christophe Tela wrote:

Pour stocker un prix, mieux vaux utiliser un BigDecimal. Avec un float, tu
t'exposes à des problèmes d'arrondi.


merci pour l'info, je dois changer cela ...
--
une bévue