You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

73 lines
1.7 KiB

package;
import Common;
/**
* Formatting tools used on both client and server side.
*
* @author fbarbut
*/
class Formatting
{
/** smart quantity filter : display easier-to-read quantity when it's floats
*
* 0.33 x Lemon 12kg => 2kg Lemon
*/
public static function smartQt(orderQt:Float,productQt:Float,unit:Unit):String{
return formatNum(orderQt * productQt) + " " + Formatting.unit(unit);
}
public static function formatNum(n:Float):String {
if (n == null) return "";
//arrondi a 2 apres virgule
var out = Std.string(roundTo(n, 2));
//ajout un zéro, 1,8-->1,80
if (out.indexOf(".")!=-1 && out.split(".")[1].length == 1) out = out +"0";
//virgule et pas point
return out.split(".").join(",");
}
/**
* Round a number to r digits after coma.
*/
public static function roundTo(n:Float, r:Int):Float {
return Math.round(n * Math.pow(10,r)) / Math.pow(10,r) ;
}
public static function parseFloat(s:String):Float{
if(s.indexOf(",")>0){
return Std.parseFloat(StringTools.replace(s,",","."));
}else{
return Std.parseFloat(s);
}
}
/**
* Display a unit
* @param u -
*/
public static function unit(u:Unit):String{
/*t = sugoi.i18n.Locale.texts;
if(u==null) return t._("piece||unit of a product)");
return switch(u){
case Kilogram: t._("Kg.||kilogramms");
case Gram: t._("g.||gramms");
case Piece: t._("piece||unit of a product)");
case Litre: t._("L.||liter");
case Centilitre: t._("cl.||centiliter");
}*/
if(u==null) return "pièce(s)";
return switch(u){
case Kilogram: "Kg.";
case Gram: "g.";
case Piece: "pièce(s)";
case Litre: "L.";
case Centilitre:"cl.";
}
}
}