Nano decimal notation within price object transactions api error through numbers beginning with 0
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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 represtend 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 aloud.. 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?
actions-on-google
add a comment |
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 represtend 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 aloud.. 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?
actions-on-google
add a comment |
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 represtend 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 aloud.. 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?
actions-on-google
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 represtend 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 aloud.. 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?
actions-on-google
actions-on-google
edited Jan 3 at 21:51
Prisoner
36k33562
36k33562
asked Jan 3 at 20:20
ChristopherDChristopherD
263
263
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
};
}
Thanks. That's clear. But how do I create then these number from 01 to 99 when they are strings? As I use .split to split the price up.
– ChristopherD
Jan 3 at 21:25
Question and answer updated
– Prisoner
Jan 3 at 21:51
This works like a charm. Thanks!
– ChristopherD
Jan 3 at 22:33
Great! If it has helped, accepting and/or upvoting the answer is always appreciated.
– Prisoner
Jan 3 at 22:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029253%2fnano-decimal-notation-within-price-object-transactions-api-error-through-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
};
}
Thanks. That's clear. But how do I create then these number from 01 to 99 when they are strings? As I use .split to split the price up.
– ChristopherD
Jan 3 at 21:25
Question and answer updated
– Prisoner
Jan 3 at 21:51
This works like a charm. Thanks!
– ChristopherD
Jan 3 at 22:33
Great! If it has helped, accepting and/or upvoting the answer is always appreciated.
– Prisoner
Jan 3 at 22:38
add a comment |
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
};
}
Thanks. That's clear. But how do I create then these number from 01 to 99 when they are strings? As I use .split to split the price up.
– ChristopherD
Jan 3 at 21:25
Question and answer updated
– Prisoner
Jan 3 at 21:51
This works like a charm. Thanks!
– ChristopherD
Jan 3 at 22:33
Great! If it has helped, accepting and/or upvoting the answer is always appreciated.
– Prisoner
Jan 3 at 22:38
add a comment |
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
};
}
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
};
}
edited Jan 3 at 21:50
answered Jan 3 at 20:51
PrisonerPrisoner
36k33562
36k33562
Thanks. That's clear. But how do I create then these number from 01 to 99 when they are strings? As I use .split to split the price up.
– ChristopherD
Jan 3 at 21:25
Question and answer updated
– Prisoner
Jan 3 at 21:51
This works like a charm. Thanks!
– ChristopherD
Jan 3 at 22:33
Great! If it has helped, accepting and/or upvoting the answer is always appreciated.
– Prisoner
Jan 3 at 22:38
add a comment |
Thanks. That's clear. But how do I create then these number from 01 to 99 when they are strings? As I use .split to split the price up.
– ChristopherD
Jan 3 at 21:25
Question and answer updated
– Prisoner
Jan 3 at 21:51
This works like a charm. Thanks!
– ChristopherD
Jan 3 at 22:33
Great! If it has helped, accepting and/or upvoting the answer is always appreciated.
– Prisoner
Jan 3 at 22:38
Thanks. That's clear. But how do I create then these number from 01 to 99 when they are strings? As I use .split to split the price up.
– ChristopherD
Jan 3 at 21:25
Thanks. That's clear. But how do I create then these number from 01 to 99 when they are strings? As I use .split to split the price up.
– ChristopherD
Jan 3 at 21:25
Question and answer updated
– Prisoner
Jan 3 at 21:51
Question and answer updated
– Prisoner
Jan 3 at 21:51
This works like a charm. Thanks!
– ChristopherD
Jan 3 at 22:33
This works like a charm. Thanks!
– ChristopherD
Jan 3 at 22:33
Great! If it has helped, accepting and/or upvoting the answer is always appreciated.
– Prisoner
Jan 3 at 22:38
Great! If it has helped, accepting and/or upvoting the answer is always appreciated.
– Prisoner
Jan 3 at 22:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029253%2fnano-decimal-notation-within-price-object-transactions-api-error-through-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown