Within the transactions API documention:
https://developers.google.com/actions/reference/rest/Shared.Types/Price
it's stated the price of a product must be represented in the following format:
{
"currencyCode": string,
"units": string,
"nanos": number
}
Where the nanos represent the decimals in the price. So 18.98 euro will be:
{
"currencyCode": EUR,
"units": 18,
"nanos": 980000000
}
But here is the problem. How can I represent for example 18.07 euro. I would say:
{
"currencyCode": EUR,
"units": 18,
"nanos": 070000000
}
But the case is that numbers beginning with 0 are not allowed. So we're a bit stuck here how we can manage this.
Given the original price as a string ("18.07", for example), how can we get the units and nanos in the correct form?
You don't need to show exactly 9 digits. You just need a number that, when divided by 10^9 (or when you move the decimal point 9 places to the left, if you'd rather), represents the decimal portion of the units you need.
So your example can be written as
{
"currencyCode": "EUR",
"units": "18",
"nanos": 70000000
}
You don't specify what language you're using, but if you're doing this in JavaScript, you can use something like this to pass two strings (the price and the currency code) and get the Price object back:
function price( p, cc ){
// Split the string on a decimal point, if present
let pa = p.split(".");
let units = pa[0];
// If we had something after the decimal point, add enough 0s to
// make sure it represents nanos, then turn it into a number
// by parsing it as a base-10 integer.
let nanos = 0;
if( pa.length > 1 ){
let ns = pa[1]+"000000000";
ns = ns.substring(0,9);
nanos = parseInt( ns, 10 );
}
return {
currencyCode: cc,
units: units,
nanos: nanos
};
}