Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
price as string, number to string, price lv, lv_LV
<?php
/**
* Class PriceToString
*
* Author name: TAHQ69
* Author: http://blog.graphicstories.net/?author=2
* URL: http://blog.graphicstories.net/?p=174
*/
class PriceToString {
/**
* @var array
*/
public static $_1_2 = [
1 => "viens",
2 => "divi",
];
/**
* @var array
*/
public static $_1_19 = [
1 => "viens",
2 => "divi",
3 => "trīs",
4 => "četri",
5 => "pieci",
6 => "seši",
7 => "septiņi",
8 => "astoņi",
9 => "deviņi",
10 => "desmit",
11 => "vienpadsmit",
12 => "divpadsmit",
13 => "trīspadsmit",
14 => "četrpadsmit",
15 => "piecpadsmit",
16 => "sešpadsmit",
17 => "septiņpadsmit",
18 => "astoņpadsmit",
19 => "deviņpadsmit",
];
/**
* @var array
*/
public static $ten = [
2 => "divdesmit",
3 => "trīsdesmit",
4 => "četrdesmit",
5 => "piecdesmit",
6 => "sešdesmit",
7 => "septiņdesmit",
8 => "astoņdesmit",
9 => "deviņdesmit",
];
/**
* @var array
*/
public static $hundred = [
1 => "simt",
2 => "divi simti",
3 => "trīs simti",
4 => "četri simti",
5 => "pieci simti",
6 => "seši simti",
7 => "septiņi simti",
8 => "astoņi simti",
9 => "deviņi simti",
];
/**
* @var array
*/
public static $thousand = [
1 => "tūkstotis",
2 => "tūkstoši",
3 => "tūkstoši",
];
/**
* @var array
*/
public static $million = [
1 => "miljons",
2 => "miljoni",
3 => "miljoni",
];
/**
* @var array
*/
public static $billion = [
1 => "miljards",
2 => "miljardi",
3 => "miljardi",
];
/**
* @var array
*/
public static $eiro = [
1 => "eiro",
2 => "eiro",
3 => "eiro",
];
/**
* @var array
*/
public static $eirocent = [
1 => "eirocents",
2 => "eirocenti",
3 => "eirocenti",
];
/**
* @var string
*/
public static $after_billion = " ";
/**
* @var string
*/
public static $after_million = " ";
/**
* @var string
*/
public static $after_thousand = " ";
/**
* @var string
*/
public static $after_hundred = " ";
/**
* @var string
*/
public static $after_ten = " ";
/**
* @var string
*/
public static $before_cent = " ";
/**
* @var string
*/
public static $after_cent = " ";
/**
* @param float $L
*
* @return string
*/
public static function convert( $L )
{
$_1_2 = static::$_1_2;
$_1_19 = static::$_1_19;
$ten = static::$ten;
$hundred = static::$hundred;
$s = " ";
$s1 = " ";
$cent_all = $L * 100;
$cent = intval( $L ) * 100;
$all = intval( $cent_all - $cent );
$L = intval( $L );
if( $L >= 1000000000 )
{
$many = 0;
static::semantic( intval( $L / 1000000000 ), $s1, $many, 3, $_1_2, $_1_19, $ten, $hundred );
$s .= $s1 . static::$billion[ $many ] . static::$after_billion;
$L %= 100000000;
}
if( $L >= 1000000 )
{
$many = 0;
static::semantic( intval( $L / 1000000 ), $s1, $many, 2, $_1_2, $_1_19, $ten, $hundred );
$s .= $s1 . static::$million[ $many ] . static::$after_million;
$L %= 1000000;
if( $L == 0 )
$s .= static::$eiro[ 3 ];
}
if( $L >= 1000 )
{
$many = 0;
static::semantic( intval( $L / 1000 ), $s1, $many, 1, $_1_2, $_1_19, $ten, $hundred );
$s .= $s1 . static::$thousand[ $many ] . static::$after_thousand;
$L %= 1000;
if( $L == 0 )
$s .= static::$eiro[ 3 ];
}
if( $L != 0 )
{
$many = 0;
static::semantic( $L, $s1, $many, 0, $_1_2, $_1_19, $ten, $hundred );
$s .= $s1 . static::$eiro[ $many ];
}
if( $all > 0 )
{
$many = 0;
static::semantic( $all, $s1, $many, 1, $_1_2, $_1_19, $ten, $hundred );
$s .= static::$before_cent . $all . static::$after_cent . static::$eirocent[ $many ];
}
else
$s .= " 00 " . @static::$eirocent[ 3 ];
return $s;
}
/**
* @param $i
* @param $words
* @param $fem
* @param $f
* @param $_1_2
* @param $_1_19
* @param $ten
* @param $hundred
*/
public static function semantic( $i, &$words, &$fem, $f, $_1_2, $_1_19, $ten, $hundred )
{
$words = "";
if( $i >= 100 )
{
$jkl = intval( $i / 100 );
$words .= $hundred[ $jkl ] . static::$after_hundred;
$i %= 100;
}
if( $i >= 20 )
{
$jkl = intval( $i / 10 );
$words .= $ten[ $jkl ] . static::$after_ten;
$i %= 10;
}
switch( $i )
{
case 1:
$fem = 1;
break;
case 2:
case 3:
case 4:
$fem = 2;
break;
default:
$fem = 3;
break;
}
if( $i )
{
if( $i < 3 && $f > 0 )
$words .= $f >= 2 ? $_1_19[ $i ] : $_1_2[ $i ];
else
$words .= $_1_19[ $i ];
$words .= static::$after_ten;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment