Skip to content

Commit

Permalink
percentages for each person
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazuh committed Mar 22, 2024
1 parent 0b6d74f commit 337665e
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 33 deletions.
91 changes: 78 additions & 13 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5673,15 +5673,16 @@
]))
]));
};
var $author$project$Main$centsToPriceString = function (cents) {
var whole = (cents / 100) | 0;
var $author$project$Main$fakeIntToDecimalStrOfTwoPlaces = function (value) {
var whole = (value / 100) | 0;
var whileStr = $elm$core$String$fromInt(whole);
var fractional = cents - (whole * 100);
var isGlitchedNumber = !_Utils_eq(value, value);
var fractional = value - (whole * 100);
var fractionalStr = (fractional < 10) ? ('0' + $elm$core$String$fromInt(fractional)) : $elm$core$String$fromInt(fractional);
return whileStr + ('.' + fractionalStr);
return isGlitchedNumber ? '0.00' : (whileStr + ('.' + fractionalStr));
};
var $author$project$Main$centsToPriceWithCurrency = function (cents) {
return '$ ' + $author$project$Main$centsToPriceString(cents);
return '$ ' + $author$project$Main$fakeIntToDecimalStrOfTwoPlaces(cents);
};
var $elm$html$Html$em = _VirtualDom_node('em');
var $elm$html$Html$li = _VirtualDom_node('li');
Expand Down Expand Up @@ -5736,18 +5737,18 @@
var peopleQtt = $elm$core$List$length(model.people);
var peopleCalcsForItems = A2(
$elm$core$List$map,
function (it) {
function (calcs) {
return {
id: it.id,
nameWithDefault: it.nameWithDefault,
subtotalInCents: it.subtotalInCents,
subtotalWithCurrency: $author$project$Main$centsToPriceWithCurrency(it.subtotalInCents)
id: calcs.id,
nameWithDefault: calcs.nameWithDefault,
subtotalInCents: calcs.subtotalInCents,
subtotalWithCurrency: $author$project$Main$centsToPriceWithCurrency(calcs.subtotalInCents)
};
},
A2(
$elm$core$List$filter,
function (it) {
return (!(!it.id)) || ((!it.id) && (it.subtotalInCents > 0));
function (calcs) {
return (!(!calcs.id)) || ((!calcs.id) && (calcs.subtotalInCents > 0));
},
A2(
$elm$core$List$map,
Expand All @@ -5772,6 +5773,26 @@
var totalTipInCents = subtotalInCents * 0.1;
var totalInCents = subtotalInCents + $elm$core$Basics$floor(totalTipInCents);
var eachTipInCents = $elm$core$Basics$floor(totalTipInCents / peopleQtt);
var sharingsInPercentage = A2(
$elm$core$List$map,
function (calcs) {
return {
id: calcs.id,
nameWithDefault: calcs.nameWithDefault,
percentual: $author$project$Main$fakeIntToDecimalStrOfTwoPlaces(calcs.percentualInt),
percentualInt: calcs.percentualInt
};
},
A2(
$elm$core$List$map,
function (calcs) {
return {
id: calcs.id,
nameWithDefault: calcs.nameWithDefault,
percentualInt: $elm$core$Basics$floor(((calcs.subtotalInCents + eachTipInCents) * 10000) / totalInCents)
};
},
peopleCalcsForItems));
return A2(
$elm$html$Html$ul,
_List_Nil,
Expand Down Expand Up @@ -5878,6 +5899,50 @@
$elm$html$Html$text(
$author$project$Main$centsToPriceWithCurrency(totalInCents))
]))
])),
A2(
$elm$html$Html$li,
_List_fromArray(
[
A2($elm$html$Html$Attributes$style, 'margin-bottom', '8px')
]),
_List_fromArray(
[
$elm$html$Html$text('Relative payment share (including tips):'),
A2(
$elm$html$Html$ul,
_List_Nil,
A2(
$elm$core$List$map,
function (result) {
return (!result.id) ? A2(
$elm$html$Html$li,
_List_Nil,
_List_fromArray(
[
A2(
$elm$html$Html$em,
_List_Nil,
_List_fromArray(
[
$elm$html$Html$text('Everyone: ')
])),
A2(
$elm$html$Html$span,
_List_Nil,
_List_fromArray(
[
$elm$html$Html$text(result.percentual + '%')
]))
])) : A2(
$elm$html$Html$li,
_List_Nil,
_List_fromArray(
[
$elm$html$Html$text(result.nameWithDefault + (': ' + (result.percentual + '%')))
]));
},
sharingsInPercentage))
]))
]));
};
Expand Down Expand Up @@ -6102,7 +6167,7 @@
_List_Nil,
_List_fromArray(
[
$elm$html$Html$text('Here\'s how to pay. Percentages may have been rounded.')
$elm$html$Html$text('Here\'s how to pay. Some values might have been rounded from decimals with too many places.')
])),
$author$project$Main$viewResultsList(model)
])),
Expand Down
80 changes: 60 additions & 20 deletions src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import Html.Attributes exposing (for, href, id, maxlength, placeholder, step, st
import Html.Events exposing (onClick, onInput)



-- TODO: each person participation in %


main : Program () Model Msg
main =
Browser.sandbox { init = init, update = update, view = view }
Expand Down Expand Up @@ -231,7 +227,7 @@ view model =
, section [ id "results" ]
[ h2 [] [ text "Results" ]
, p []
[ text "Here's how to pay. Percentages may have been rounded."
[ text "Here's how to pay. Some values might have been rounded from decimals with too many places."
]
, viewResultsList model
]
Expand Down Expand Up @@ -457,13 +453,13 @@ viewResultsList model =
, subtotalInCents = personTotalInCents person.id
}
)
|> List.filter (\it -> it.id /= 0 || (it.id == 0 && it.subtotalInCents > 0))
|> List.filter (\calcs -> calcs.id /= 0 || (calcs.id == 0 && calcs.subtotalInCents > 0))
|> List.map
(\it ->
{ id = it.id
, nameWithDefault = it.nameWithDefault
, subtotalInCents = it.subtotalInCents
, subtotalWithCurrency = centsToPriceWithCurrency it.subtotalInCents
(\calcs ->
{ id = calcs.id
, nameWithDefault = calcs.nameWithDefault
, subtotalInCents = calcs.subtotalInCents
, subtotalWithCurrency = centsToPriceWithCurrency calcs.subtotalInCents
}
)

Expand All @@ -480,6 +476,24 @@ viewResultsList model =

totalInCents =
subtotalInCents + floor totalTipInCents

sharingsInPercentage =
peopleCalcsForItems
|> List.map
(\calcs ->
{ id = calcs.id
, nameWithDefault = calcs.nameWithDefault
, percentualInt = floor ((toFloat calcs.subtotalInCents + toFloat eachTipInCents) * 10000 / toFloat totalInCents)
}
)
|> List.map
(\calcs ->
{ id = calcs.id
, nameWithDefault = calcs.nameWithDefault
, percentualInt = calcs.percentualInt
, percentual = calcs.percentualInt |> fakeIntToDecimalStrOfTwoPlaces
}
)
in
ul
[]
Expand Down Expand Up @@ -516,6 +530,25 @@ viewResultsList model =
[ text "Total bill: "
, strong [] [ text (centsToPriceWithCurrency totalInCents) ]
]
, li [ style "margin-bottom" "8px" ]
[ text "Relative payment share (including tips):"
, ul []
(List.map
(\result ->
if result.id == 0 then
li []
[ em [] [ text "Everyone: " ]
, span [] [ text (result.percentual ++ "%") ]
]

else
li []
[ text (result.nameWithDefault ++ ": " ++ result.percentual ++ "%")
]
)
sharingsInPercentage
)
]
]


Expand All @@ -524,17 +557,25 @@ priceToCents amount =
floor (amount * 100)


centsToPriceString : Int -> String
centsToPriceString cents =
centsToPriceWithCurrency : Int -> String
centsToPriceWithCurrency cents =
"$ " ++ fakeIntToDecimalStrOfTwoPlaces cents


fakeIntToDecimalStrOfTwoPlaces : Int -> String
fakeIntToDecimalStrOfTwoPlaces value =
let
isGlitchedNumber =
value /= value

whole =
cents // 100
value // 100

whileStr =
String.fromInt whole

fractional =
cents - (whole * 100)
value - (whole * 100)

fractionalStr =
if fractional < 10 then
Expand All @@ -543,12 +584,11 @@ centsToPriceString cents =
else
String.fromInt fractional
in
whileStr ++ "." ++ fractionalStr
if isGlitchedNumber then
"0.00"


centsToPriceWithCurrency : Int -> String
centsToPriceWithCurrency cents =
"$ " ++ centsToPriceString cents
else
whileStr ++ "." ++ fractionalStr


personNameWithDefault : Person -> String
Expand Down

0 comments on commit 337665e

Please sign in to comment.