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.

72 lines
1.7 KiB

  1. package;
  2. import Common;
  3. /**
  4. * Formatting tools used on both client and server side.
  5. *
  6. * @author fbarbut
  7. */
  8. class Formatting
  9. {
  10. /** smart quantity filter : display easier-to-read quantity when it's floats
  11. *
  12. * 0.33 x Lemon 12kg => 2kg Lemon
  13. */
  14. public static function smartQt(orderQt:Float,productQt:Float,unit:Unit):String{
  15. return formatNum(orderQt * productQt) + " " + Formatting.unit(unit);
  16. }
  17. public static function formatNum(n:Float):String {
  18. if (n == null) return "";
  19. //arrondi a 2 apres virgule
  20. var out = Std.string(roundTo(n, 2));
  21. //ajout un zéro, 1,8-->1,80
  22. if (out.indexOf(".")!=-1 && out.split(".")[1].length == 1) out = out +"0";
  23. //virgule et pas point
  24. return out.split(".").join(",");
  25. }
  26. /**
  27. * Round a number to r digits after coma.
  28. */
  29. public static function roundTo(n:Float, r:Int):Float {
  30. return Math.round(n * Math.pow(10,r)) / Math.pow(10,r) ;
  31. }
  32. public static function parseFloat(s:String):Float{
  33. if(s.indexOf(",")>0){
  34. return Std.parseFloat(StringTools.replace(s,",","."));
  35. }else{
  36. return Std.parseFloat(s);
  37. }
  38. }
  39. /**
  40. * Display a unit
  41. * @param u -
  42. */
  43. public static function unit(u:Unit):String{
  44. /*t = sugoi.i18n.Locale.texts;
  45. if(u==null) return t._("piece||unit of a product)");
  46. return switch(u){
  47. case Kilogram: t._("Kg.||kilogramms");
  48. case Gram: t._("g.||gramms");
  49. case Piece: t._("piece||unit of a product)");
  50. case Litre: t._("L.||liter");
  51. case Centilitre: t._("cl.||centiliter");
  52. }*/
  53. if(u==null) return "pièce(s)";
  54. return switch(u){
  55. case Kilogram: "Kg.";
  56. case Gram: "g.";
  57. case Piece: "pièce(s)";
  58. case Litre: "L.";
  59. case Centilitre:"cl.";
  60. }
  61. }
  62. }